[erlang-questions] type syntax question

Kostis Sagonas <>
Thu Oct 30 09:05:44 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?

The deviceOpHandler() does not need a "type" declaration.  In fact, it 
shouldn't have one for just the above.  In the old spec syntax, the 
above example can simply be written as:

   -spec(okHandler/2 :: (integer(), cstate()) -> cstate()).

or, from R12B-4 onwards, more simply as:

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

which is now the recommended way -- the EEP needs to be updated.

If at some other function you need to specify that the function takes a 
deviceOpHandler as an argument, then you can define that type as:

   -type deviceOpHandler() :: fun((integer(), cstate()) -> cstate()).

and use it as:

   -spec my_foo(deviceOpHandler()) -> 42.


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

Well, the answer depends on your definition of "soon"... ;-)

The current plan is that types & specs will be properly documented in R13.

Kostis



More information about the erlang-questions mailing list