This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-05-24
Channels
- # aleph (1)
- # beginners (43)
- # calva (22)
- # cider (51)
- # clerk (1)
- # clj-kondo (20)
- # clojure (29)
- # clojure-denmark (1)
- # clojure-europe (73)
- # clojure-finland (28)
- # clojure-nl (1)
- # clojure-norway (7)
- # clojure-spec (7)
- # clojure-uk (4)
- # clojurescript (12)
- # data-science (2)
- # datomic (51)
- # events (1)
- # fulcro (20)
- # hyperfiddle (28)
- # integrant (6)
- # malli (20)
- # matrix (2)
- # music (1)
- # off-topic (66)
- # reitit (17)
- # releases (5)
- # ring (1)
- # shadow-cljs (31)
- # xtdb (6)
Hello everyone 🙂 Is there a way to implement IWithMeta & IMeta for primitives (#object[Number], #object[Boolean], #object[Function], etc...)?
ClojureScript 1.11.60
cljs.user=> (meta 1)
nil
cljs.user=> (extend-type number IMeta (-meta [n] (println 'meta n)))
#object[Function]
cljs.user=> (meta 1)
meta 1
nil
you can fake it but it won't behave properly, since primitives don't have "instances".
Ah yeah, in my answer I focused only on the "implement a protocol for primitives" side of it.
another small question: can someone explain the following?:
(satisfies? IWithMeta +) ;; => false
(with-meta + {:hello "world"}) ;; works
How does +
support with-meta yet fail satisfies?
, is there a more reliable way to test for support of with-meta
(except try
& catch
)?I think the source is pretty much self-describing:
ClojureScript 1.11.60
cljs.user=> (source with-meta)
(defn with-meta
"Returns an object of the same type and value as obj, with
map m as its metadata."
[o meta]
(if (js-fn? o)
(MetaFn. o meta)
(when-not (nil? o)
(-with-meta o meta))))
🙏 2