My apologies, I just realized that there is a clojurescript group. I will
ask my question there.

2015-06-15 23:02 GMT-04:00 Damien Lepage <damienlep...@gmail.com>:

> Hello,
>
> I found the following technique in order to have a record inheriting a
> base behavior, 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
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to