Erlang Language Behaviours Defining a behaviour


Example

You can define your own behaviour by adding -callback directives in your module. For example, if modules implementing your behaviour need to have a foo function that takes an integer and returns an atom:

-module(my_behaviour).
-callback foo(integer()) -> atom().

If you use this behaviour in another module, the compiler will warn if it does not export foo/1, and Dialyzer will warn if the types are not correct. With this module:

-module(bar).
-behaviour(my_behaviour).
-export([foo/1]).

foo([]) ->
    {}.

and running dialyzer --src bar.erl my_behaviour.erl, you get these warnings:

bar.erl:5: The inferred type for the 1st argument of foo/1 ([]) is not a supertype of integer(), which is expected type for this argument in the callback of the my_behaviour behaviour
bar.erl:5: The inferred return type of foo/1 ({}) has nothing in common with atom(), which is the expected return type for the callback of my_behaviour behaviour