I enjoy experimenting with programming paradigms and trying out some interesting (to me) ideas (some things become posts, like this and that). Recently I decided to see if I can write object-oriented code in a functional language.

Idea

I was trying to get some inspiration from Alan Kay, the creator of object-oriented programming.

OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things.

I decided that I will be happy if I can implement message sending and inner state.

So here comes the main problem of the whole concept — the state.

State

You are not supposed to have a state in functional programming. So how do you change values in functional programming? Usually, by using recursion (pseudocode):

function list_sum(list, result)

if empty?

result

else

list_sum(tail(list), result + first(list)) list_sum([1, 2, 3, 4], 0)

In imperative programming, we usually create a variable and change its value all the time. Here we, essentially, are doing the same by calling a function again with different parameters.

But an object should have a state and receive messages. Let’s try this:

function some_object(state)

message = receive_message()

next_state = process_message(message)

some_object(next_state)

Seems reasonable to me. But this is blocking everything, how do I create other objects? How do I send messages between them? Well, let me quote Alan Kay again:

I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages

This gave me an idea of using parallelism. I called some_object(state) function “object loop” and decided to run it in a separate thread. The only mystery left so far is messaging.

Messaging

In terms of messaging, I decided that we can just use channels (they seem to be incredibly popular in Go programming language). Then receive_message() would be just waiting for a message appearing in a channel (message queue). Sounds easy enough.

Language

Initially, I wanted to use Haskell but I don’t know the language so I’d have a really hard time dealing with laziness, static typing and tons of googling when all I wanted really was just to make a prototype of my idea. So I decided to use Clojure since it’s dynamic and is great for interactive programming (which makes life a lot easier for prototyping and experimenting).

Now, being mixed-paradigm language, it’s possible to have an actual state in Clojure:

(def user (atom {:id 1, :name "John"}))

@user ; ==> {:id 1, :name "John" }

(reset! user {:id 1, :name "John Doe"})

@user ; ==> {:id 1, :name "John Doe"}

We will be avoiding using stuff like this, of course.

Object

So, the core concept of object-oriented programming is an object. Things like classes are not required (for example, JavaScript, being OOP language, doesn’t have classes; It emulates them, being prototype-oriented). Let’s start by implementing objects.

So, what do we need for our object? So far I’ve mentioned “object loop” and channels. Also, we need process_message(message) function.

Clojure has its own implementation of channels in clojure.core.async library so we will be using it. But first, we need to think of data-structure for representing our object. It’s simple, really:

Now we just need to add an object loop:

async/<!! is just a function that waits for the message in a channel. A message handler is supposed to return next state or nil to stop the loop. Also, the message handler is supposed to accept the object itself (self), state and, of course, the message as arguments.

Okay, everything is ready, all we need is to glue it together — initialise an object:

Here we literally just start the loop and return a data structure for other code to communicate with that. And other code can communicate with the object by sending it a message via send-msg . async/>!! , as you might guess, writes something to a channel.

Using objects

Okay, but does it work? Let’s try it out. I decided to test it on string builder.

String builder is just an object that combines a bunch of strings together:

builder = new StringBuilder

builder.add "Hello"

builder.add " world"

builder.build # ===> "Hello world"

So let’s try it implementing it:

(this is a bit modified version from the test I wrote)

So, we can think of the message handler as a dispatcher that calls the proper method depending on a message received. Here we have 5 methods.

Let’s try our hello world example:

The first two lines are quite straightforward. But what’s going on after?

Our object lives in a separate thread and it’s supposed to return the object state. So how can we get some result from it? By using callbacks and promises.

Here I decided to use a callback and deliver a promise in it. I don’t think it’s a good design and I should have used promises right away instead. However, this is just for demonstration so sue me :)

@result-promise is fetching the result of a promise and if it’s not ready, it waits for it (blocks the current thread).

Now, add-twice is a bit more interesting, because it sends messages to self. One of the problems with this object design is that we can’t really call other methods from a method, because the object loop deals with one message at a time. So we can only work with other methods asynchronously. It’s just a limitation of this architecture and it should be kept in mind or objects can get stuck.

When I was testing it, I did something like this:

1. call :add-twice with "ha" string

2. call :build and see if it equals "haha"

and it didn’t work. This is because :build message was sent before :add-twice sent :add messages (it’s a queue, remember?).

I spent quite a bit of time trying to understand what was wrong. That happened because I am not used to parallel programming and it is a very common issue. This is one of the reasons why functional programming is getting popular nowadays — pure functions make it much harder to make a mistake like that. My state just had a race condition. States is evil ;)

So, that was our foundation of an object system. We can build a lot of things on top of this. Let’s do classes, shall we?

Classes

For me, a class is just a template of an object, containing its behaviour (methods). And, to be honest, classes can be objects themselves (as they are in Ruby, for example). So let’s introduce some classes.

First, we need to “standardise” how methods are called and executed. I’m getting lazy so I will just dump the whole namespace here (apologies):

So, a message to call a method is just a hashmap containing two things: method name and the arguments it should be called with.

Also, take a look at for-message function. I am getting a bit ahead of myself but we will supply classes with hashmaps name => method containing methods. The function execute defines the way methods are executed — instead of accepting message it accepts arguments so we don’t have to think about messages in our methods.

Handling messages is now quite straightforward:

Now, let’s take a look at what classes look like:

As you can see, I decided to create classes as objects. I didn’t have to, classes could be more abstract concept but I thought it’s just funnier this way. We could go even further and make new-klass function private and create an object klass instead that could create other classes via the method :new . It’s quite simple, actually, but I decided not to push it that far.

Anyway, our klasses are just objects with a state containing methods, constructor (for initialising new instances) and a vector with instances. We don’t actually need the vector, but why not.

Now, what is instantiate function serving as the method :new ? Here it is:

So when we creating a new instance, the constructor is being used for the initial state and the instance is added to the vector mentioned earlier. The object is being delivered via promise.

Also, I added a helper function for synchronised instantiation:

Okay, let’s try creating a class-oriented string-builder.

Noice!

More?

This is just a prototype and it has many flaws (no error handling, objects can get stuck, memory is leaking). But there are so many things we could implement. For example, inheritance. Or we could go into the prototype-oriented way. Another thing we could do is to write a nice DSL for that and it can turn out to be really nice since we are using Clojure here.

Also, we have mixins available for free already. Mixins are just method maps, merged in when instantiating a new class.

Can we build something useful with it?

I made a simple showcase app — TODO list (classic). It has 3 classes: todo list, todo list item and CLI. You can see the code in the repo (the link is below). I will just say that it was quite straight-forward. Here’s the console output:

# add

Title: Buy lots of toilet paper # add

Title: Make a TODO list # list

TODO list:

- Buy lots of toilet paper

- Make a TODO list # complete

Index: 1 # list

TODO list:

- Buy lots of toilet paper

+ Make a TODO list # exit

(the cursive font indicates the input from a keyboard)

Conclusion

Well, that was interesting (to me). Along the way, I was trying to understand if that prototype can be translated to Haskell. I can’t say for sure but I think it’s possible. Haskell has channels, promises and parallelism. Even if it didn’t, we could always expand on the idea of an object and instantiate objects as separate processes and send messages with something like RabbitMQ.

For me, the most fascinating thing about programming paradigms is that they are so different and yet absolutely the same. It’s not about the language, it’s about the way a programmer thinks. Languages just allow us to code in a certain style much easier and more productive.

I hope my shabby writing wasn’t completely boring and maybe you even learned something:)

You can find the repo with the showcase and some tests here.