Using -spec for callbacks when defining behaviours

Magnus Henoch magnus@REDACTED
Wed Mar 17 17:26:38 CET 2010


Kostis Sagonas <kostis@REDACTED> writes:

> The extension is fully backwards compatible. The old format is still
> supported (the clause of the behaviour_info function can contain both
> pairs and triples).

I'd call that "forwards compatible" :)

If I write a file in the new format, and pass it to an "old" compiler, I
get a warning about a bad callbacks definition, and I don't get any
warnings about missing behaviour functions:

%% foo.erl
-module(foo).
-compile(export_all).

behaviour_info(callbacks) ->
    [{foo, 1, "-spec foo(A) -> A."},
     {bar, 0}].

%% bar.erl
-module(bar).
-behaviour(foo).
-export([baz/1]).

baz(A) ->
    A.

%%

$ erlc foo.erl
$ erlc bar.erl
./bar.erl:2: Warning: behaviour foo callback functions erroneously defined

I.e., I get no warnings, neither for foo/1 (new-style) nor bar/0 (old-style).

How about using a separate clause of the behaviour_info function for
type specs?  Like this:

%% foo.erl
-module(foo).
-compile(export_all).

behaviour_info(callbacks_with_types) ->
    [{foo, 1, "-spec foo(A) -> A."},
     {bar, 0, "-spec bar() -> 'ok'."}];
behaviour_info(callbacks) ->
    [{F,A} || {F,A,_Spec} <- behaviour_info(callbacks_with_types)].

%%

With bar.erl being the same, I get:

$ erlc bar.erl
./bar.erl:2: Warning: undefined callback function bar/0 (behaviour 'foo')
./bar.erl:2: Warning: undefined callback function foo/1 (behaviour 'foo')

which is what I'd like to see.

-- 
Magnus Henoch, magnus@REDACTED
Erlang Solutions
http://www.erlang-solutions.com/
---------------------------------------------------

---------------------------------------------------

WE'VE CHANGED NAMES!

Since January 1st 2010 Erlang Training and Consulting Ltd. has become ERLANG SOLUTIONS LTD.

www.erlang-solutions.com



More information about the erlang-questions mailing list