[erlang-questions] type syntax question

Tobias Lindahl tobias.lindahl@REDACTED
Thu Oct 30 09:35:51 CET 2008



Anthony Shipman wrote:
> I want to write something like
> 
> -type (deviceOpHandler() :: fun( (integer(), cstate()) -> cstate() )).
> 
> -spec (okHandler/2 :: deviceOpHandler()).
> 
> okHandler(_DeviceID, CState) ->
>     sendResponse(200, "OK", CState).
> 
> but I can't find the right combination of parentheses to please the compiler.
> Is this kind of specification possible?

No it isn't. You can use your own declared types in specs (of course), 
but for the top level in the spec you have to be explicit. There is 
nowadays also an alternative notation to the specs that do away with 
some of the parentheses.

-spec okHandler(integer(), cstate()) -> cstate().

I find this one easier to read. As before you can also put your 
variables in the arguments if you like.

-spec okHandler(DeviceID :: integer(), CState :: cstate()) -> cstate().

> 
> Will there be documentation on the type and spec syntax coming soon?
> 

Please define soon... ;-)

Well, I guess when the EEP gets accepted and the types become an 
official feature someone should make a proper documentation. For now, 
you can look in the EEP http://www.erlang.org/eeps/eep-0008.html which 
is in need of an update, but gets the main things correct.

Tobias





More information about the erlang-questions mailing list