FPOO Chapter 5: Classes

Note: This article is part of a series working through the exercises in Brian Marick's "Functional Programming for the Object-Oriented Programmer", in Elixir instead of Clojure. The full notes and source code are on GitHub.

So far, my tiny object system in Elixir has been stowing all methods directly in instances. Chapter 5 of FPOO directs me to move instance methods out into a “class” of some kind.

First off, there’s no more new_point . In its place, a simple keyword list defining attributes of the class. The instance variables are no longer hard-coded in the keyword list. Instead, there is a new add_instance_values callback that plays the role of an initializer method.

Next up, a new make function that can use this style of class definition.

Now for message dispatch.

I gotta say, I kind of prefer Elixir pipelines for chaining keyword gets:

…to Clojure nested function calls:

(EDIT: I had either forgotten or didn’t yet know about subscript ( []) access when this was written)

Before I go any further, let’s see if any of this is working.

Exercise 1: apply-message-to

A small refactoring to pull out this helper method:

Now the make and send_to functions can be refactored to use this new helper method.

Exercise 2: class and class-name

The behavior I want:

This necessitates a change to the class definition: