Erlang Language Behaviours Defining a behaviour

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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


Got any Erlang Language Question?