[erlang-questions] Erlang/OTP R15B has been released

Kostis Sagonas kostis@REDACTED
Thu Dec 15 11:39:52 CET 2011


On 12/14/2011 06:05 PM, Kenny Stone wrote:
> How do "behaviour callbacks" differ from the way it used to work?

In a nutshell, behaviour callbacks allow behaviour modules to specify 
(using a formal language, instead of just in comments) the names and 
types of all functions that callback modules should implement. This 
information can then be processed by dialyzer (but in the future 
presumably also by other tools, e.g. edoc) to check conformance of a 
callback module with what the behaviour module expects and warn the user 
for errors that may exist.

The syntax of -callback attributes is the same as that of -spec 
attributes. The main difference is that -callback attributes refer to 
functions that do not belong to the module containing them (and cannot 
be found in its code) but to the callback module(s).

Note the callback module can provide more specific implementations of 
what the behaviour module expects. For example, a gen_server expects 
that the callback module exports an init/1 function. In R15B, the 
relevant callback attribute reads:

   -callback init(Args :: term()) ->
       {ok, State :: term()} |
       {ok, State :: term(), timeout() | hibernate} |
       {stop, Reason :: term()} | ignore.

but a specific callback module may decide to provide an init function 
that accepts an empty list as Args and only returns {ok, State}.  Thus, 
in the callback module, one may want to provide the following spec:

   -spec init([]) -> {ok, state()}.

for some locally defined state() type.

Kostis

PS. We'll try to add this in Section 1.2 of the OTP Design Principles
User's Guide.  Currently, some of this info has been added in a weird 
place of the Guide that is hard to find...  Apologies for this.



More information about the erlang-questions mailing list