[erlang-questions] Supervision Tree

Garrett Smith <>
Wed Nov 28 16:29:37 CET 2012


On Wed, Nov 28, 2012 at 4:23 AM, Lucky Khoza <> wrote:
> Hi Erlang Developers,
>
> I'm actually trying to understand and compile the following module code, and
> i don't understand why the init/1 function takes an argument which it
> doesn't use at all, so when i try to compile the module it complains about
> the argument _Args because it's unused.
>
> May someone help me understand the use of this argument?
>
> -module(ch_sup).
> -behaviour(supervisor).
>
> -export([start_link/0]).
> -export([init/1]).
>
> start_link() ->
>     supervisor:start_link(ch_sup, []).
>
> init(_Args) ->
>     {ok, {{one_for_one, 1, 60},
>           [{ch3, {ch3, start_link, []},
>             permanent, brutal_kill, worker, [ch3]}]}}.
>

As Bengt pointed out, the leading underscore for the arg will tell the
compiler to not complain. It doesn't change the meaning of the
variable, however -- it will still be bound to the value used in the
function call, you can use it expressions, etc.

As for why it takes an argument in the first place, the function is a
callback that's part of a "behavior" contract. This is an important
pattern in Erlang -- anytime you declare that a module implements a
behavior, you're stating that it exports a list of functions with
specific arities (argument count). If you don't export what's expected
of the behavior, the compiler will complain.

If you don't happen to use an argument, in a behavior callback or any
function, add an underscore to the name, as you have, and all will be
well.

Garrett



More information about the erlang-questions mailing list