ensure_started
Joe Armstrong
joe@REDACTED
Mon Mar 24 12:49:06 CET 2003
On Fri, 21 Mar 2003, Peter H|gfeldt wrote:
>
> On Feb 1, 1997 I wrote the following, addressed to the OTP developing
> team. I think it is still valid.
>
> /Peter
>
> "Sometimes I see code like the following (in real applications or in
> examples in documents):
>
> start(Name, Module, Args) ->
> case whereis(Name) of
> undefined ->
> Pid = spawn(Module, init, Args),
> register(Name, Pid),
> Pid;
> Other ->
> Other
> end.
>
> That is bad programming:
>
... etc.
Indeed it is :-) - I often wonder why we made spawn/3 a primitive and
not just spawn a "universal" empty process and then send it a message
telling it what to do.
Pid = spawn()
Pid ! Fun()
where
spawn() ->
spawn(fun() ->
receive
Fun ->
Fun()
end
end).
This is particularly useful when you need to setup sets of
mutually recursive processes, then you can say:
Pid1 = spawn()
Pid2 = spawn(),
Pid1 ! fun() -> ... Pid2 ... end,
Pid2 ! fun() -> ... Pid1 ... end,
/Joe
More information about the erlang-questions
mailing list