April 26th, 2015
In Technology
No Comments
If you enjoy this article, see the other most popular articles
If you enjoy this article, see the other most popular articles
If you enjoy this article, see the other most popular articles
When to use reify in Clojure
(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.
This is a nice explantation. If you have a protocol, then you can instantiate with a record, like this:
(defprotocol Foo (bar [this]) (baz [this st]) (quux [this x y])) (defrecord FooRecord Foo (bar [this] (println this)) (baz [this st] (str this st)) (quux [this x y] (str this (* x y))))
But if only need to instantiate the protocol one time, you can skip defining a record and just use reify instead:
(def athing (reify Foo (bar [this] (println this)) (baz [this st] (str/replace this (re-pattern st))) (quux [this x y] (str this (+ x y)))))
If you are sure you only need to use the protocol once, then reify is perfect. Of course, the question comes up, if you only need to use the protocol once, then why do you need a protocol? That’s a rare case in Clojure — at least so far, it is has not been common for libraries to force clients to fulfill interfaces.
Post external references
- 1
http://decomplecting.org/blog/2014/10/29/reify-this/
February 8, 2022 9:33 am
From Michael S on How I recovered from Lyme Disease: I fasted for two weeks, no food, just water
"Did you have Bartonella, too? Seems it uses autogenesis..."