It is annoying that extend does not exist in Clojurescript. The implementation 
of protocols has changed some since that post, but I'm not sure if that changed 
extend's feasibility.

In the meantime, you are not doing anything so dynamic that you cannot do it 
with extend-type.  To share implementations, just make a function and call it 
from within the protocol implementation:

(defn contents-impl [value]
  (map (fn [[key value]] (MyMapEntry. key value)) value))

(extend-type PersistentArrayMap
  Styled
  (contents [value] (contents-impl value)))




On Wednesday, May 4, 2016 at 10:31:02 AM UTC-5, Nate Young wrote:
> I have a protocol that a generic rendering method uses:
> 
> 
> ```
> (defprotocol Styled
>   (css-class [this])
>   (contents [this]))
> ```
> 
> 
> and I want plain cljs maps to implement it. As I have constantly re-learned, 
> there are two map types, so I currently have two `extend-type` clauses:
> 
> 
> ```
> (extend-type PersistentArrayMap
>   Styled
>   (css-class [_]
>     "fig-map")
> 
> 
>   (contents [value]
>     (map (fn [[key value]]
>            (MyMapEntry. key value))
>          value)))
> 
> 
> (extend-type PersistentHashMap
>   Styled
>   (css-class [_]
>     "fig-map")
> 
> 
>   (contents [value]
>     (map (fn [[key value]]
>            (MyMapEntry. key value))
>          value)))
> ```
> 
> 
> but this makes changes to how maps implement `Styled` a bit perilous and 
> tedious. In Java-land, I could lean on `clojure.core/extend`:
> 
> 
> ```
> (let [map-styled
>       {:css-class (constantly "fig-map")
>        :contents (fn [value]
>                    (map (fn [[key value]]
>                           (MapEntry. key value))
>                         value))}]
>   (extend PersistentArrayMap
>     Styled
>     map-styled)
> 
> 
>   (extend PersistentHashMap
>     Styled
>     map-styled))
> ```
> 
> 
> but this doesn't appear to be a thing in ClojureScript.
> 
> 
> Is there a language feature that allows for concise reuse of protocol 
> implementations across types? Or am I abusing protocols/creating my own 
> problems by attempting to extend the myriad built-in Clojure types to a 
> protocol? (and how should I be framing a solution)?
> 
> 
> 
> 
> I did find this mailing list topic from a few years back:
> https://groups.google.com/forum/#!topic/clojurescript/GZypP_YV4l0
> which I read as saying that `extend` is tricky to implement (and I think 
> `specify` might be missing the mark for my use case).
> 
> 
> Thanks!

-- 
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 https://groups.google.com/group/clojurescript.

Reply via email to