[boilerplate bypath=”fp-oo”]

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:

Published by Avdi Grimm

Leave a Reply

Your email address will not be published. Required fields are marked *