I'm trying to define two versions of a macro, one for Clojure and one for
Clojurescript, using reader conditionals.
I end up seeing the Clojure version in Clojurescript. I assume this happens
because the macro definition is processed by Clojure, before Clojurescript
runs, so it's the #?(:clj ...) version that's compiled rather than the :cljs
version. Then the :clj macro is what is then available in Clojurescript.
(a) Is there a way to do what I'm trying to do without doing somersaults
through twisty hoops?
(b) If I'm invoking Clojure to compile Clojurescript, shouldn't the compiler
know that I want the :cljs macro definition, not the one wrapped in :clj?
Example:
#?(:clj (defmacro inv
"Matrix inversion."
[x]
`(mx/inverse ~x))
:cljs (defmacro inv
"Kludgey matrix inversion."
[x]
`(case (mx/shape ~x)
nil (/ 1 ~x)
[2 2] (inv22 ~x)
(throw
(js/Error. (str "Matrix inversion unavailable for " ~x))))))
Here mx/inverse is core.matrix's inverse function (not available in
Clojurescript with the aljabr implementation of core.matrix), and inv22 is my
simplistic inverse macro for 2x2 matrices.
When I use inv on a 2x2 matrix in Clojurescript (at a figwheel repl), I get an
error indicating that core.matrix's inverse function is being called.
(I first tried embedding the reader conditional inside a single macro
definition for inv. That had the same problem, so I tried the above method.)
--
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.