[erlang-questions] Erlang modelling question

Ben Butler-Cole ben@REDACTED
Mon Jun 25 20:15:38 CEST 2007


Darius Bacon said:
> If the goal is to code mutable objects with inheritance,
> the most straightforward way seems to be with a process
> for each level of inheritance for each object.

Another way of doing something similar is to use a sort of strategy module as below. These different approaches will be suitable in different circumstances. This multiple processes approach will better if the 'subclass' needs to maintain its own state.

Ben


%%%-------------

-module(animal).

create(Module) ->
    spawn(fun() -> loop(Module) end).
loop(Module)
    receive
        {talk, From} -> From ! Module:talk(), loop(Module)
    end.


%%%-------------


-module(sheep).

create() ->
    animal:create(sheep).
talk() ->
    baa.


%%%------------


-module(cow).

create() ->
    animal:create(cow).
talk() ->
    moo.







More information about the erlang-questions mailing list