[erlang-questions] Erlang shell crashes

Logan, Martin Martin.Logan@REDACTED
Mon Oct 30 18:15:54 CET 2006


Jay wrote:

Also verify that for every message send (i.e., Pid ! Msg) there is
somewhere
that you receive the message.  If you are using receive patterns that do
not
match up, messages will accumulate that can never be removed.

----------------------------------------------------

The messages a process can receive and understand is called its
alphabet.  If you use modules as the basic unit of encapsulation for an
alphabet you will minimize errant messaging.  Try to write modules that
contain only a single process definition, and don't let your alphabet
for that process leak outside of that module.  

Bad
---

-module(messager).

send(Msg, Pid) -> Pid ! {msg, Msg}.


-module(messagee).

loop(State) ->
	receive 
		{msg, Msg} -> do_stuff(Msg);
		Error -> handle_error(Msg)
	end
...
	loop(StatePrime)
	      

Good
----

-module(encapsulate)

send(Msg) ->
	registered_name_of_this_process ! {?MODULE, Msg}.

loop(State) ->
	receive
		{?MODULE, Msg} -> do_stuff(Msg)
	end
...
	loop(StatePrime).
	

You can see with the second version, the good version, that the process
only accepts messages sent from within the same module with the tag
?MODULE.  If you follow this idiom you will effectively reduce problems
with errant messaging.

Cheers,
Martin




More information about the erlang-questions mailing list