[erlang-questions] OO programming style in Erlang?
Mats Cronqvist
mats.cronqvist@REDACTED
Mon Jan 22 13:11:46 CET 2007
Kostis Sagonas wrote:
> 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.
i personally prefer the import statement in cases like this;
-import(orddict,[new/0,store/3]).
the code becomes;
new(),
store(...)
mats
More information about the erlang-questions
mailing list