Theoretically Stuck

Rudolph van Graan <>
Sat Sep 2 21:33:55 CEST 2006


Reading Yariv's blog: http://yarivsblog.com/articles/2006/09/01/oo-fp- 
erlang-and-me

In my experience I can sum up the difference between Functional  
Programming and Object Oriented programming in two sentences.

In OO it is trivial to model data and more difficult to customize  
behaviour. In FP (erlang) behaviour modeling is trivial and data  
modeling becomes a challenge.

You need both in one way or another in all languages. I've found it  
very easy to build systems where the behaviour of something can be  
customized, but really challenging (or impossible) to model data  
elegantly.

How do you write code that wraps the concept/data of a person? A  
record with name and surname fields is the obvious answer:

-record(person,{name,surname}).

 From now on until forever, a person is a 3-tuple.

Now I want to make a version of person with an extra field, say  
telephone number. Not all records have this. Some have a work address  
as well. The first record type is already in mnesia and I cannot  
change it, nor do I want to.  So I add a proplist into the #person  
record. And a type... simple_person, person_with_phone and then I add  
all the extra data into that proplist:

-record(person,{name,surname,type=simple_person,stuff=[]}).

I push all my extra data into stuff, because it is still a person.  
This is not elegant. In Java I would have had no issues whatsoever.  
PersonWithNumber inherits from Person etc. But I like erlang, I want  
a solution. I can think of tons of ways to fake this or work around  
it. But the language must help me and it doesn't. So I make  
"versioned" or "namespaced" records by parse transform. Now I cannot  
match anymore. Perhaps I can probably build a funky-macro-come-parse- 
transform that will solve this...

Lisp have the concept of classes. It is a functional language...

Less critical but nuisance anyways is that in Erlang that I cannot  
"inherit" or reuse code. I instinctively go into grrrrrr mode when I  
see two functions in different places that are the same. The same bug  
twice.

In my world most gen_servers have the same code somewhere. A logging  
function. A common initialization routine to register with other  
components. The code is exactly the same. The only way to reuse it,  
is to move the code into another module and call it. But I still have  
to write the call in each and every gen_server. You can compare the  
fact that all gen_servers have a handle_call and a handle_cast  
function with an interface in OO. Erlang's work better for me, I can  
change things easily. How can I make my_special_gen_server have all  
the code automatically for a gen_server without me having to go  
lookup the callbacks every time and go into copy-and-paste mode?

These are by far the biggest challenges for me with Erlang. You  
cannot inherit data. You cannot easily reuse code. But in the end  
there is no elegant solution in erlang. Or is there Joe and Ulf?

HELP!

Rudolph



More information about the erlang-questions mailing list