[erlang-questions] Programming Erlang book code question

Ladislav Lenart <>
Mon Oct 22 09:55:24 CEST 2007


(some of) my answers to your questions are inlined below...

Aaron Feng wrote:
> The following code snippet appeared on page 352:
> -module(sellaprime_supervisor).
> -behaviour(supervisor). % see erl -man supervisor
> -export([start/0, start_in_shell_for_testing/0, start_link/1, init/1]).
> start() -> spawn(fun() ->
>                         supervisor:start_link({local,?MODULE}, ?MODULE, 
> _Arg = [])
>                         end).
> start_in_shell_for_testing() ->
>   {ok, Pid} = supervisor:start_link({local,?MODULE}, ?MODULE, _Arg = []),
>   unlink(Pid).
> %% rest of the code intentionally left out
> Question 1:
> What's the benefit to call spawn for supervisor:start_link in the 
> start() function?  Why not just call supervisor:start_link without the 
> spawn?  Doesn't supervisor:start_link already spawn and register the 
> process already?

I don't understand it either.

> Question 2:
> What's the benefit for start_in_shell_for_testing() function?  Why not 
> just call start() function when testing in the shell?  I know what the 
> unlink(Pid) does, but why unlinking it?  What if unlink it left out?

When you cause an error in the shell (syntax or other), the shell is
killed (and restarted). However when the supervisor is started from
the shell, it will be linked to it and will be killed as well because
the supervisor thinks that its parent supervisor (the shell) exited
and is obliged to do the same.

> Question 3:
> I don't understand _Arg = [] ?  why not just pass [] as the last 
> argument of start_link?

The only reason that comes to my mind is the readability of the code.
This way you know that an empty list is actually an argument list.

Hope this helps,

Ladislav Lenart

More information about the erlang-questions mailing list