This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-06-28
Channels
- # babashka (10)
- # beginners (140)
- # cider (6)
- # clj-kondo (10)
- # cljs-dev (39)
- # cljsrn (6)
- # clojars (1)
- # clojure (23)
- # clojure-europe (2)
- # clojure-spec (7)
- # clojure-uk (6)
- # clojurescript (1)
- # conjure (16)
- # cursive (3)
- # datomic (3)
- # emacs (6)
- # fulcro (13)
- # graalvm (3)
- # malli (8)
- # meander (4)
- # off-topic (43)
- # pathom (1)
- # pedestal (15)
- # re-frame (13)
- # reagent (3)
- # sci (25)
- # shadow-cljs (26)
- # sql (9)
- # testing (34)
- # tools-deps (80)
is it possible to extend a protocol defined in another library ? …. specifically TraceSpan
in io.pedestal.log
so that it implements
?
(defprotocol AsCloseable
(-as-closeable [_]))
(extend-type java.io.Closeable
AsCloseable
(-as-closeable [this] this))
you can't actually implement a protocol on something based on it implementing another protocol
if you want to do that you probably would have to do a custom dispatch mechanism in some way
(defn as-closeable [item]
(cond
(satisfies? TraceSpan (type item))
(...)
(implements? Closeable item)
item
:else
...AAAA...))
i actually consider this a pretty big downside to protocols - they don't actually work like traits in rust or similar where you can implement a trait for anything that implements a trait
(def extending-protocols (atom [])
(defn register-impl-for-protocol! [protocol impl]
(swap! extending-protocols conj [protocol impl]))
(defn as-closeable [item]
(cond
(instance? Closeable item)
item
:else
(loop [impls @extending-protocols]
(if-not (empty? impls)
(let [[protocol impl] (first impls)
remaining-impls (rest impls)]
(if (satisfies? protocol item)
(impl item)
(recur remaining-impls)))
(throw (IllegalArgumentException. (str "No Impl found for " item)))))