Parts 1 through 5 of this series can be found here, here, here, here and here.
OK, to be honest I almost forgot that I was doing this! We’re in the home stretch now. It’s a little hard to follow the fascinating Dwemthy’s Array, but the rest of Chapter 6 is pretty interesting in its own right. Here _why shows us method_missing
(something rightfully missing in Clojure), string interpolation, and an interesting little bit about eval
in example #30.
Chapter 6 (Sections 4-5)
It’s probably possible to assemble something like method_missing
in Clojure, but it would be considered dangerous and not very practical. method_missing
in Ruby is something you define for a particular class, giving that class some instructions for how to handle calls to methods it doesn’t have. The closest analog Clojure has to classes would be records and protocols, but protocols require you to explicitly declare any methods they provide.
From a practical standpoint, you probably won’t ever run into a situation, programming in Clojure, where you would need the type of functionality that method_missing
provides. But for the sake of translating this example, a multimethod with a default implementation would be kind of similar in spirit, in that you can define custom behavior for when a particular method hasn’t been implemented:
The key difference between something like the above and Ruby’s method_missing
is that the code above constrains the “default method” behavior to when the multimethod call
is, well, called. By contrast, method_missing
is a more generalized operation that changes the behavior of an entire class… this can quickly lead to unpredictable and unsafe code.
For any interested parties, here’s some food for thought about method_missing
and Clojure.
Interestingly, Eduardo Julián has come up with Jormungandr, a library that provides a prototype-based object oriented functionality to Clojure. In researching how one might implement method_missing
in Clojure, I stumbled upon this conversation in which Eduardo introduced his library and Mikera proposed making some modifications and implementing the ability to add a custom method to each object that would execute when the object is passed a method it doesn’t have… sound familiar?
String interpolation doesn’t come baked into Clojure, although some Clojurians have come up with libraries that will do it in a way more like Ruby’s "#{syntax}."
Personally, I don’t think this is too unwieldy:
It’s about the same amount of typing as #{this syntax}
.
The “bats!” example features a demonstration of Ruby’s %(long string here)
syntax, which functions exactly like a double-quoted string. There is no equivalent in Clojure, but that isn’t really a big deal. You can always just use quotes, even if your string spans multiple lines:
Notice that you still have to escape double-quotes within the string, in contrast to Ruby’s %( )
syntax… of course, eval
also works differently in Clojure, so it’s a moot point. Observe!
This might be a little nitpicky, but I noticed that _why’s use of the PreEventualist.searchfound
function here is not entirely consistent with what the function actually does. The way it’s being used here, it’s supposed to search the PreEventualist
database and return the contents of the page as one long, multi-line string. However, when we defined this function back in ex. 10, it actually did a little more than that. Not only does it grab the content of the query results page, it also splits it into an array of individual results! So that will save us a little work for this example. Now all we have to do is filter out the results that don’t contain “truck.”
The next chapter should be fun… can’t wait!