[erlang-questions] OO programming style in Erlang?

Ladislav Lenart lenartlad@REDACTED
Wed Jan 24 13:49:28 CET 2007


Richard A. O'Keefe wrote:
> Ladislav Lenart <lenartlad@REDACTED> wrote:
> 
> 	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...
> 	
> There are basically three reasons why this is not an issue.
> 
> In Erlang programming you just don't change
> data structures that much.  There's a slogan I've seen to the effect
> that "it's better to have 100 functions operating on 1 data structure
> than 10 functions each on 10 data structures", but I've been unable to
> find the source.

That's a good one.

> Where you *do* change data structures you use Erlang records (which try
> to look like Haskell and ML records) and if you are simply adding or
> reordering fields no source changes are required.

Yes, I use them but don't like the syntax very much (it
is very obvious that it is only a compiler feature and
not a full data type).

> Finally, while a data structure implemented by some module (such as
> sets or dictionaries) *can* be manipulated directly by external code,
> in practice nobody is stupid enough to do it.  As long as clients only
> use the official exported functions, changes to the data structure
> representation are no trouble at all.

No, this is not what I was worrying about. It seemed
strange to me that I have to know exactly which function
I need to invoke on a particular item. In Smalltalk, I
just write

   "aCollection can be anything - List, Set, Dictionary, ..."
   aCollection collect: [:each | each something].

because aCollection knows what kind of collection it
actually is. In Erlang, I have to know

   lists:map(...)
   dict:map(...)
   orddict:map(...)

Furthemore lists:map expects fun/1 while dict/orddict:map
expects fun/2.

I got some good suggestions to deal with this issue,
wrapper functions and macros. The thing I don't like
is that I have to solve it myself...

But these are details really, I was just curious.

> I note that Smalltalk and Java expose everything (via reflection) and
> so does C++ (Object * can be cast to void * and thence to anything you
> want).  You are used to languages (possibly Eiffel or Ada?) where
> programmers *can't* access the implementation of data structures;
> in C++, Java, Smalltalk and Erlang programmers can but *don't*.

I am used to Smalltalk so I can but don't... :-)

Ladislav Lenart




More information about the erlang-questions mailing list