Selective receive (RE: Structs (was RE: Record selectors))

Sven-Olof Nystr|m <>
Mon Jan 20 16:02:48 CET 2003

Ulf Wiger writes:
 > >It seems to me that matching receive is just a way to
 > >compensate for Erlang's inability to create temporary
 > >channels...
 > I can't say whether Erlang ended up with pattern matching on
 > one message queue because Joe, Robert, Mike et al found it
 > too difficult to figure out how to create temporary
 > channels. (:

The implementation is not that hard. Maybe they thought that it would
be superfluous to have an implicit channel for each process AND
the ability to create temporary channels.

 > I think a single message queue and pattern matching go well
 > in hand with state-oriented programming. Again, the POTS
 > example:
 > speech(OtherPid) ->
 >     receive
 >         {lim, onhook} ->
 >             lim:disconnect_from(OtherPid),
 >             OtherPid ! {self(), cancel},
 >             idle();
 >         {lim, {digit, _Digit}} ->
 >             speech(OtherPid);
 >         {OtherPid, cancel} ->
 >             wait_on_hook(false);
 >         {Pid, request_connection} ->
 >             Pid ! {self(), reject},
 >             speech(OtherPid);
 >         Other ->
 >             speech(OtherPid)
 >     end.
 > Here, we react to messages from 4 different senders
 > (consuming and ignoring any message from anyone else). One
 > could of course imagine an equally concise syntax that
 > scanned separate channels, pattern-matching over them,
 > blocking if desired and necessary.

Yes. This could be written with a receive operation that simply grabs
the next message in the mailbox + a case statement.

 > I find very little wrong with the notion of having a single
 > message queue.

Matching receive is a rather strange construct. If arbitrary
expressions were allowed in guards, it would be possible to write a
receive that counted the messages in a mailbox. Unlike case
statements, there is no way to translate a receive into simpler things
(this affected the design of Core Erlang). As far as I know, no other
programming language has anything similar.

[Comment on OHaskell deleted.]

Sven-Olof Nyström

More information about the erlang-questions mailing list