[erlang-questions] Parse-transforming !! to a function call

David Mercer <>
Fri Aug 24 17:05:52 CEST 2007


On Friday, August 24, 2007 at 03:34 Mats Cronqvist wrote:
>    in any case, i tend to use this idiom a lot, and i think it does 
> indeed indicate a missing language feature.
> 
> safe_send(Pid,Msg) ->
>    Ref = erlang:monitor(process,Pid),
>    Pid ! {Ref,self(),Msg},
>    receive
>      {'DOWN',Ref,_,_,R} ->
>         exit({no_receiver,R,Pid});
>      {Ref,Ans} ->
>         erlang:demonitor(Ref),
>         receive {_,Ref,_,_,_} -> ok after 0 -> ok end,
>         Ans
>    end.

I like your use of Ref to identify which message is the reply to your
request.  I was using make_ref/0 before for this, but your way is better.
Your demonitor-receive instructions can be condensed into one line:

	erlang:demonitor(Ref, [flush])

Cheers,

David




More information about the erlang-questions mailing list