ANNOUNCE ex11 release two

Shawn Pearce spearce@REDACTED
Fri Jan 23 02:20:00 CET 2004


Joe Armstrong <joe@REDACTED> wrote:
> I have experimented with many different styles of callback handling
> and IMHO this is the best. If you want to do it with messages you can write 
> your top-level thing like this:
> 
> 	B = swButton:make(Win ......)
> 	S = self(),
> 	B ! {onClick, fun(X) -> S ! {wrapper, X} end},
> 	...
> 
> 	loop(...).
> 
>         
>         loop(...) ->
> 	   receive
> 		{wrapper, X} ->
> 		    Do something
> 	        ...
> 	   end

What about allowing the caller to use a term for the message
if they just want a message sent:

	B = swButton:make(Win ....),
	S = self(),
	B ! {onClick, {S, wrapper} end},

	loop () ->
		receive
		{wrapper, X} ->
		end,
		loop().

Easy to tell a tuple and a fun apart; if it is a tuple it is a
two tuple of {Pid, Tag}; which results in the message {Tag, X}
being sent to to Pid.  This way the caller doesn't need to
build a fun, and waste all the bytecode and memory on a fun to
just send itself a tagged message.

Of course, this is only really useful if a large number of apps
are written this way.  :)

-- 
Shawn.

  Getting there is only half as far as getting there and back.



More information about the erlang-questions mailing list