Style with gen_server

Luke Gorrie luke@REDACTED
Thu Jun 17 18:07:28 CEST 1999


Hi guys,

I'm having my first poke around with gen_server, and I have a question 
about the style its used with.  I also note that what I'm trying to do 
is fairly "object-oriented", so if there's a different mindset which
would be more appropriate for this problem in Erlang, please let me
know. :)

Basically, what I want to do is implement polymorphism.  The neatest
way to do it seems to be using anonmyous gen_server processes which
all expect the same sorts of messages, and then build a client module
to encapsulate the gen_server:call stuff.  So, I'll have some modules:

my_client:      no 'behaviour'.  maps calls like wash(Pid, Dishes) to
                gen_server:call(Pid, {wash, Dishes}, infinity).

dishwasher:     implements gen_server behaviour.  used as with:
                {ok,Pid} = gen_server:start_link(dishwasher,[],[]),
                my_client:wash(Pid, SomeDishes).
slave:          as above, but with a different implementation

Is this the right way to go?  Sorry if this seems terribly obvious, I
just want to check that I'm thinking the right way. :)

Also, I suppose that if I wanted throw an exception at the caller, the 
best way would be to stick something in my_client like:

wash(Pid,Dishes) ->
  case gen_server:call(Pid, {wash,Dishes}) of
    {ok,Result} -> Result;
    {error,Reason} -> throw(Reason)
  end.

Right?

Cheers,
Luke





More information about the erlang-questions mailing list