[erlang-questions] OO programming style in Erlang?

Ladislav Lenart lenartlad@REDACTED
Tue Jan 23 18:30:53 CET 2007


Sean Hinde wrote:
>>
>> When I started the thread I didn't want to ask "How can
>> I write OO in Erlang" (the mail has a bad subject) but
>> rather "How to write properly in Erlang" which is what
>> I have stll trouble with (at least it seems to me that
>> way). But I guess that this, as everything, takes time
>> to learn...
> 
> One great way to pick up the idioms is to study code by the masters. The 
> OTP sources can be a great source of inspiration and examples of the 
> "right way".
> 
> Quite a bit (most) of the code in Jungerl also probably qualifies. Yaws 
> is high profile but probably not my favourite example of good erlang 
> style (too much use of get/put (yagpb) ).
> 
> mnesia is quite awesome, but rather heavyweight as a learning tool.
> 
> Suggestions for "Best Practice" open source erlang projects anyone?

Yes, this is certainly a good start. I will definitely
take a look at (some of) them.

>> Regarding OO, there is only a few programming languages
>> that are "OO enough for me" (picking only those I know):
>> Smalltalk, CLOS. I've also read some articles about Self
>> and it looks quite good but I have never programmed in it.
> 
> A certian Lisp/Smalltalk hacker round these parts keeps telling me that 
> Smalltalk is something like Erlang in concept, but I never quite 
> listened hard enough to see why ;-)

I'd say it is actually quite true. Joe Armstrong had a
presentation on this (well, last) year's ESUG (European
Smalltalk User Group Conferrence). In the begining of
his presentation, he said:
   "Erlang is Smalltalk Alan Kay wanted it to be."
and all the (presumably Smalltalk) audience laughed... :-)

But from a point of view of an experienced Smalltalker,
I must say that the shift from Smalltalk to Erlang is not
so trivial, mainly because of a quite complex and fixed
syntax. I got quite used to the idea of minimal language
syntax and extendable libraries over the years. And this
I miss a lot in Erlang. A simple example to illustrate my
point follows (maybe you will even tell me a simple way
how to do something like this in Erlang?)...

Let's suppose I have to write a lot of code like this
in Smalltalk:

   anObject isNil
     ifFalse: [anObject doSomething].

I can "extend the language" by providing a new method
which takes a block of code as its only argument:

   Object>>ifNotNilDo: oneArgBlock
     "This is the branch where object (self in this
      context) is not nil and so the block is evaluated
      (with the object as its argument)."
     ^oneArgBlock value: self.

   UndefinedObject>>ifNotNilDo: oneArgBlock
    "This is the branch where object (self in this
     context) is actually nil and so the block is ignored
     and nil is returned."
     ^self.

Now I can replace the code above with shorter and more
readable form:

   anObject ifNotNilDo: [:obj | obj doSomething].

Sorry for the Smalltalk excursion... :-)

Ladislav Lenart




More information about the erlang-questions mailing list