Looking for OOP in Erlang is like looking for religious icons in a cheese sandwich
Thursday, August 23rd, 2007Recently there’s been a lot of discussion around how processes in Erlang could be considered to be objects and that Erlang can therefore be considered an object oriented language. I really think that’s stretching the definition. They’re kind of right, but in the same way that the people who saw a vision of the Virgin Mary in a cheese sandwich were right.
Way back in 2004 you may remember some crazy woman announced she had a 10 year old toasted cheese sandwich which miraculously had the face of the Virgin Mary cooked onto it. The sandwich eventually sold on ebay for $28,000, which just added to the circus factor.
I’m not going to get into a theological debate over snack food, but I really just see a random burn marking. But then I’m not particularly religious, so I’m not looking for images of the virgin in the first place. If you were a devout Catholic with a love of toasted treats, you might be. If you were a Beatles fan, you might see the face of John Lennon or Ringo Starr.
As for me, I did see a kiwifruit with a mark the shape of New Zealand the other day. But then I am a New Zealander and kiwifruits come from New Zealand, so I’m more inclined to see that. Other people might miss it.
People look for patterns that are already familiar to them. When they encounter new things, first instinct is to apply existing knowledge to it. It happens with cheese sandwiches and kiwi fruit, and it happens with programming languages.
First up, Ralph Johnson wrote in Erlang, the next Java
Processes certainly support data abstraction and polymorphism. An Erlang process is a function that reads from the incoming message queue, pattern matches to find a particular message, and then responds to it. A function structured in this particular way is similar to a class in Smalltalk. Moreover, given several kinds of processes that have a common protocol and that share some things in common, it is easy to factor out the commonality into a function that they can both call. This is similar to class inheritance. So, you could even say that Erlang supports inheritance, though it does it very differently than in Java or Smalltalk. I imagine that many Erlang programmers think about programming as modeling. So, Erlang fits all the characteristics of an OO system, even though sequential Erlang is a functional language, not an OO language.
Kirit Sælensminde picked that up and ran with it in Erlang as an OO language.
As I’ve been looking more and more at Erlang it strikes me more and more that there is an object oriented language hiding just below the surface.
Chris Petrilli also commented in Object-oriented processes comparing Joe Armstrong’s comments on how Erlang is not considered to be OO, and comments by Dr. Alan Kay on the original definition of OO.
So, messaging is a core of both OOP and what Armstrong calls Concurrency Oriented Programming. In both cases, the concept of messages are used as a strong isolation mechanism to keep one piece from peering into another and fiddling with the internals. In the case of Smalltalk, for example, that piece is an Object. In Erlang, its a Process.
So are these guys right? Well, yeah. Technically. It’s more than possible to shoehorn an OO mindset into Erlang. I have to admit I thought exactly the same thing when I started looking at processes receiving and acting on messages. You can definitely apply OO thinking to the concept, and yes I have be doing mostly OO work for the last few years. This linking is a pretty useful human skill for picking up new knowledge, but you have to be careful though to stop and ask how much of that is accurate, and how much is from the mental filters inherent from previous experience.
You can apply pretty much any programming metaphor to any language you like if you shine the light the right way and squint hard enough: You can do Object Oriented programming in C if you want (it’s ugly and involves passing lots of “this” structs, but you can do it); you can do functional programming in Java (kind of, but without most of the power and elegance), or even duck-typing if you love your keyboard; and you can even fake a static typing system in Ruby if you’re a masochist. Does that mean those languages are actually productive writing code in that style? No. Sure it’s an interesting academic comparison, but of little value at the end of the day except to highlight the shortcomings in using those languages in that way.
So what about the cheese sandwich?
When I say that finding the OO in Erlang is like finding the Virgin in the cheese sandwich, I mean that if you look hard enough expecting to find something, then you probably will.
This is mostly all pretty harmless, and its a big benefit to learning a new skill to build on top of existing knowledge. However, we need to be wary of the fact that there are different ways of thinking about things. If we go applying all the old thought patterns because that’s what we’re most comfortable with, we risk missing a huge opportunity to do things in new and better ways. And Erlang does definitely provide that. There are numerous valuable and unique concepts that would be missed by examining it through just through the lens of an OO expert and nothing else.
