[erlang-questions] OO programming style in Erlang?

Kostis Sagonas kostis@REDACTED
Mon Jan 22 12:44:01 CET 2007


Ladislav Lenart wrote:
> 
> [2]
> I have an OO experience and I like the basic ideas of encapsulation
> and polymorphism but can not find a way how to materialize these in
> my Erlang programs.
> 
> For example, there are dict and orddict modules which have compatible
> (polymorphic) interfaces but differ in their implementations. This is
> essentially good, but when I actually use one of these modules, my
> code will look like
> 
>    orddict:new()
>    orddict:store(...)
> 
> But this is bad because when I later decide to change (my) implementation
> I actually have to go through my code and replace orddict with dict. So
> this is not a polymorphism I am looking for.

One way of avoiding this problem is the one we follow in various modules
of the HiPE compiler.  We use the preprocessor as follows:

-define(DICT, orddict).

and then write all calls as:

   ?DICT:new()
   ?DICT:store(...)

Not really deep, but it does what we want.

Kostis



More information about the erlang-questions mailing list