Question about fun's

Shawn Pearce <>
Wed Jan 27 20:24:12 CET 1999


Ok, we're looking at implementing a system in Erlang where it would be nice to
have true object-oriented behavior.  The way this is typically done in scheme
is to make each object a lambda and bind the object state to the lambda when
you construct the object.  Thus you can do "(object print)" to make it print
out stuff.

I'm thinking that we'd do something similar in Erlang like so:

-module(myclass).
-export([new/0]).

new() -> save_state([]).

save_state(State) ->
	fun(Message) ->
		myclass:dispatch(State,Message)
	end.

dispatch(State,{print}) -> io:format("Hi!");
dispatch(State,{append,Item}) -> save_state([Item|State]);
dispatch(State,{get}) -> State;
dispatch(State,{empty}) -> save_state([]).

Then I can work with the object like this:

O=myclass:new().
O1=O({append,TheThingy}).
O1({print}).
O2=O1({empty}).

What are the drawbacks, ie is the fun going to cost me a large amount of RAM,
or a large amount of processing overhead on each message?

--
Shawn.

(The above are the rantings of a body without a mind.)




More information about the erlang-questions mailing list