[erlang-questions] OO programming style in Erlang?

Taavi Talvik taavi@REDACTED
Tue Jan 23 20:48:18 CET 2007


>> Data structures are by nature highly symbolic (the atom type) and  
>> (apart
>> from dedicated ADTs like dict and gb_trees) tend to be complex,  
>> exposed
>> and passed around and operated on directly by pure functions.
>
> And this I guess is the part I have a psychological problem with.
> I am used to an idiom where data are hidden behind an interface
> that operates on them. This decouples user of the module from
> having to deal with internals of that module. But as you described
> it, data are exposed in Erlang. So each time you want to change the
> representation (add something because of one new function), you have
> to revisit many of the existing functions that operate on that
> representation. This seems more than a few changes to me...

Yes, psychological problem or matter of discipline.

For exacmple, dict uses complex internal representation. But besides
possibility to examine (or display with io:format("~p", Dict)) internal
structure you don't need to know it.

  D0 = dict:new(),
   D1 = dict:store(files, [], D0),
   D2 = dict:append(files, f1, D1),
   D3 = dict:append(files, f2, D2),
   D4 = dict:append(files, f3, D3),
   dict:fetch(files, D4).
[f1,f2,f3]

Same with digraph, set, queue, gb_* and your own generic modules;)

If you think of modules as encapsulation units (class) and exported
functions as accessor functions, rest is unimportant internal  
representation.

Of course - for doing interesting things you have to know your own data
structures. And you pay price for doing interesting things having to  
think
about data structures and having to rewrite functions from time to time.

best regards,
taavi





More information about the erlang-questions mailing list