Anyone know a better way than ducktyping for a CLJC way of writing a predicate atom?
They are different types in CLJS (cljs.core/Atom) vs CLJ (clojure.lang.atom). It would be great if such a way also worked with Reagent Atoms.
I have the following function which I dug up from an old project which uses Reagent:
(defn atomic?
[x]
#?(:clj (instance? clojure.lang.Atom x)
:cljs (satisfies? IAtom x))
#_(or (instance? cljs.core/Atom x)
(satisfies? reagent.ratom/IReactiveAtom x)))
The commented out part captures Reagent atoms (sadly I’m not sure why I commented it out), so you could probably or
that with the :cljs part to cover both Atoms and Reagent atoms. I can’t remember if the latter implements IAtom or not. Of course if it does, then you can just use the reader conditional above as is.
Ah! I’ve tried the reader-conditional portion and it seems to be working. I haven’t seen any issues in reagent yet, but we’ll see. Thanks!
This trick also works:
(def atom?
(let [t (type (atom nil))]
(fn [x] (instance? t x))))
That’s a really good trick if all you’re interested in is Clojure/Script Atom instances, but it will return false for Reagent atoms or anything else which implements the IAtom protocol in ClojureScript.
yep RAtom implements IAtom
so
(satisfies? IAtom x)
will work.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.