[erlang-questions] Selective receive issue

Richard A. O'Keefe <>
Wed Aug 6 01:38:43 CEST 2008


On 5 Aug 2008, at 6:59 pm, Vlad Dumitrescu wrote:
> It seems to me that there is no place where I can put a '_->...;'  
> clause without creating problems. So I'd like to ask if anyone sees  
> another way to structure this that will allow rogue messages to be  
> discarded.

"Add another process."
Present a Haskeller with a problem, and he starts by saying
"I'll just define another combinator..."
Present an Erlanger with a problem, and he starts by saying
"I'm sure we can fix this by adding another process."

start_handler(X, Y, Z) ->
     Pid = spawn(fun () -> real_handler(X, Y, Z) end),
     spawn(fun() -> guardian(Pid) end).


guardian(Pid) ->
     receive
         start        -> Pid!start
       ; stop         -> Pid!stop
       ; {input,Data} -> Pid!{input,Data}
       ; _            -> discard
     end,
     guardian(Pid).

Now your real process hides behind its guardian; nobody
can possibly get a message to it except by sending it to
the guardian.

> receive
>   Pattern -> keep(), loop();
>   Msg ->  process(Msg), loop()
> end

When you pass the "->", you leave the special mailbox handling mode,
so keep() would be a very strange backwards time-travelling function.

I think that extra syntax would be needed, along the lines of

	Pattern [when Guard] [!] -> Body

with "!" (reminiscent of message sending) meaning to leave the
message in the mailbox.

The question is whether enabling a whole new class of serious
bugs is better or worse than requiring people to add an extra
process.




More information about the erlang-questions mailing list