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 clojurescript+unsubscr...@googlegroups.com.
To post to this group, send email to clojurescript@googlegroups.com.
Visit this group at https://groups.google.com/group/clojurescript.

Reply via email to