[erlang-questions] Somebody please correct my -callback understanding

17 views
Skip to first unread message

Damian Dobroczyński

unread,
Dec 14, 2011, 11:58:23 AM12/14/11
to Erlang-Questions Questions
Hi list!

I have a module "a" defining callback "c":

-module (a).
-callback c (integer()) -> integer().

Now, I have callback module "b" implementing invalid (non-comforming the
a:c/1 spec) callback "c":

-module (b).
-behavior (a).
-export ([c/1]).

c (A) when is_atom(A) ->
A.

Now, I dialyze both modules:

$ dialyze -n --src a.erl b.erl

which outputs:

Proceeding with analysis... done in 0m0.64s
done (passed successfully)

What I've expected is the dialyzer warning about invalid b:c/1 function
(something about breaking the contract for c/1). Am I right?

-- D.
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Damian Dobroczyński

unread,
Dec 14, 2011, 1:11:37 PM12/14/11
to Erlang-Questions Questions
W dniu 14.12.2011 17:58, Damian Dobroczyński pisze:

> Hi list!
>
> I have a module "a" defining callback "c":
>
> -module (a).
> -callback c (integer()) -> integer().
>
> Now, I have callback module "b" implementing invalid (non-comforming the
> a:c/1 spec) callback "c":
>
> -module (b).
> -behavior (a).
> -export ([c/1]).
>
> c (A) when is_atom(A) ->
> A.
>
> Now, I dialyze both modules:
>
> $ dialyze -n --src a.erl b.erl
>
> which outputs:
>
> Proceeding with analysis... done in 0m0.64s
> done (passed successfully)
>
> What I've expected is the dialyzer warning about invalid b:c/1 function
> (something about breaking the contract for c/1). Am I right?
>
> -- D.

Wow, It's a miracle! I've just found the answer and (what I understand)
a bug. IT IS "-behavior (...)" which is perfectly understood by the
compiler but not by by the dialyzer! Dialyzer expects "-behaviour (...)"
(note the different spelling).

Daniel Luna

unread,
Dec 14, 2011, 1:38:33 PM12/14/11
to Damian Dobroczyński, Erlang-Questions Questions
2011/12/14 Damian Dobroczyński <qoo...@gmail.com>:

> Wow, It's a miracle! I've just found the answer and (what I understand)
> a bug. IT IS "-behavior (...)" which is perfectly understood by the
> compiler but not by by the dialyzer! Dialyzer expects "-behaviour (...)"
> (note the different spelling).

This is not a bug. Erlang has always used 'behaviour' (and
'behaviour_info'). See
http://en.wikipedia.org/wiki/American_and_British_English_spelling_differences#-our.2C_-or

As a side note, Erlang allows for any user defined elements starting with -.

If you for example add the following line to your code

-my_own_local_thing(whatever_data).

then this is fully legal Erlang, and its effect is seen if you run
Module:module_info()

[...
{attributes,[{behaviour,[supervisor]},
{my_own_local_thing,[whatever_data]}]},
...]

Cheers,

Daniel

Damian Dobroczyński

unread,
Dec 14, 2011, 1:52:17 PM12/14/11
to Daniel Luna, Erlang-Questions Questions
Dnia śro, 14 gru 2011, 19:38:33 Daniel Luna pisze:

> 2011/12/14 Damian Dobroczyński <qoo...@gmail.com>:
>> Wow, It's a miracle! I've just found the answer and (what I understand)
>> a bug. IT IS "-behavior (...)" which is perfectly understood by the
>> compiler but not by by the dialyzer! Dialyzer expects "-behaviour (...)"
>> (note the different spelling).
>
> This is not a bug. Erlang has always used 'behaviour' (and
> 'behaviour_info'). See
> http://en.wikipedia.org/wiki/American_and_British_English_spelling_differences#-our.2C_-or
>
> As a side note, Erlang allows for any user defined elements starting with -.
>
> If you for example add the following line to your code
>
> -my_own_local_thing(whatever_data).
>
> then this is fully legal Erlang, and its effect is seen if you run
> Module:module_info()
>
> [...
> {attributes,[{behaviour,[supervisor]},
> {my_own_local_thing,[whatever_data]}]},
> ...]
>
> Cheers,
>
> Daniel

From Erlang Reference Manual:

"""
-behaviour(Behaviour).

The atom Behaviour gives the name of the behaviour, which can be a user
defined behaviour or one of the OTP standard behaviours gen_server,
gen_fsm, gen_event or supervisor.

The spelling behavior is also accepted."""" <--- HERE

It's a bug. Besides as I mentioned, the compiler perfectly understand
the attribute "-behavior" giving me warnings about missing callbacks.
So, compiler understand and behave correctly with "-behavior" -
dialyzer don't.

(Unfortunately (or maybe not) my code if full of "behaviors" ;) just
because it is accepted and understood not even deprecated).

-- D.

Damian Dobroczyński

unread,
Dec 14, 2011, 2:00:20 PM12/14/11
to Daniel Luna, Erlang-Questions Questions
Dnia śro, 14 gru 2011, 19:52:17 Damian Dobroczyński pisze:

I've just looked into "Erlang. Programming" by Cesarini & Thompson
(2009), page 271, an example of gen_server (usr.el listining):

(...)
-behavior(gen_server).
(...)


Well, thats enough for me to be completely comvinced it's not a weird
thing to write "-behavior (...)". I think it's in common, especially
for Americans.

Daniel Luna

unread,
Dec 14, 2011, 2:03:39 PM12/14/11
to Damian Dobroczyński, Erlang-Questions Questions
2011/12/14 Damian Dobroczyński <qoo...@gmail.com>:

You're right. My bad.

Cheers

Ulf Wiger

unread,
Dec 14, 2011, 2:07:55 PM12/14/11
to Damian Dobroczyński, Erlang-Questions Questions

If memory serves, the behavior/behaviour dichotomy was initially my fault.

I wrote the first prototype for user-defined behaviors, and no one noticed that I had used American spelling, until it was pointed out much later, after the official release. :)

BR,
Ulf W

Stavros Aronis

unread,
Dec 15, 2011, 5:03:45 AM12/15/11
to erlang-pr...@googlegroups.com, Erlang-Questions Questions
Hi Damian,

you are correct to point out that this is an omission on Dialyzer's part. I will send a patch for this soon.

Stavros

Damian Dobroczyński

unread,
Dec 15, 2011, 5:08:08 AM12/15/11
to erlang-pr...@googlegroups.com, Erlang-Questions Questions, Stavros Aronis
Dnia czw, 15 gru 2011, 11:03:45 Stavros Aronis pisze:

Thanx.

B.regards,

Reply all
Reply to author
Forward
0 new messages