Hello,
I found the following technique in order to have a record inheriting a base
behavior in Clojure, using extend: https://gist.github.com/david-mcneil/661983
Unfortunately this can't work in ClojureScript at this point, due to the lack
of extend. Is there any alternative or should I fall back to bare functions
instead of defrecord?
Maybe some context on my code would help you answer this question.
I transform some data through a pipeline of wrappers implementing this
protocol:
(defprotocol Wrapper
(wrap-data [this data]))
Some of my wrappers can be toggled on/off so I added this protocol:
(defprotocol Togglable
(toggle [this])
(on? [this]))
Such a togglable wrapper would be implemented like this:
(defrecord MyWrapper [on]
Togglable
(toggle [this] (update-in this [:on] not))
(on? [this] on)
Wrapper
(wrap-data [this data]
(if on
(do-something data)
data)))
Now I realized that my implementation for the Togglable protocol would always
be the same, so I would have liked a way to write the code only once. The only
solution I found to do that while keeping my Togglable protocol, is the
following macro:
(defmacro deftogglable [name fields & specs]
`(defrecord ~name [~@fields ~'on]
~'Togglable
(~'toggle [this#] (update-in this# [:on] not))
(~'on? [~'this] ~'on)
~@specs))
Which I can use like this:
(macro/deftogglable MyWrapper []
Wrapper
(wrap-data [this data]
(if on
(do-something data)
chart)))
I don't like this approach too much because it's very specific and wouldn't
compose well, or allow overriding part of the functions.
How would you go to solve this problem?
Thanks,
Damien
--
Note that posts from new members are moderated - please be patient with your
first post.
---
You received this message because you are subscribed to the Google Groups
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.