[erlang-questions] Erlang way for process-as-library?

Torbjorn Tornkvist <>
Wed Feb 7 21:00:22 CET 2007


I'm surprised over the long thread my rough sketch caused.
In real life I would of course do almost as in Mats proposal,
with some minor changes... :-)

Mats Cronqvist wrote:
> 
> start() ->
>    spawn(fun init/0) ! {hello,self()},
>    receive {ok,Pid} -> Pid
>    end.

start() ->
  Self = self(),
  Pid = spawn(fun() -> init(Self) end),
  receive {ok, Pid, RealPid} -> RealPid end.

> 
> init() ->
>    receive {hello,Daddy} -> ok end,
>    try register(name,self()), Daddy ! {ok,self()}
>    catch error:badarg -> Daddy ! {ok,whereis(name)},exit(normal)
>    end,
>    loop().

init(Daddy) ->
  try register(name,self()), Daddy ! {ok,self(),self()}
  catch error:badarg -> Daddy ! {ok,self(),whereis(name)},exit(normal)
  end,
  loop().


--Tobbe




More information about the erlang-questions mailing list