[erlang-questions] OO programming style in Erlang?
Richard A. O'Keefe
Wed Jan 24 00:57:07 CET 2007
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.
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.
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.
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*.
More information about the erlang-questions