I found a problem in the Chas approach. When you call a macro from a cljs
file, and that macro calls another macro with a(:ns &env) test, it sees a
Clojure &env, so it thinks is being compiled in a CLJ environment. This
doesn't happens with the Prismatic approach (the old one, it was recently
changed for the Chas one). See below:
;;;----- p/macros.clj ------
(ns p.macros)
(defn log [s]
(spit "/tmp/log" (str (pr-str s) "\n") :append true))
(defmacro if-cljs ;; Chas approach
[then else]
(log [:if-cljs-env &env])
(if (:ns &env) then else))
(defn compiling-cljs? ;; Prismatic (old) approach
[]
(boolean
(when-let [n (find-ns 'cljs.analyzer)]
(when-let [v (ns-resolve n '*cljs-file*)]
@v))))
(defmacro other-macro
[]
(log [:other-macro-env &env])
(log [:if-cljs (if-cljs :cljs :clj)])
(log [:if-compiling-cljs (if (compiling-cljs?) :cljs :clj)]))
;;;----- p/core.cljs ------
(ns p.macros
(:require-macros [p.macros]))
(p.macros/other-macro)
When you run this, you get the following contents in the /tmp/log file:
[:if-cljs-env {&env #<LocalBinding
clojure.lang.Compiler$LocalBinding@18b3f8f5>, &form #<LocalBinding
clojure.lang.Compiler$LocalBinding@7a69c3fb>}]
[:other-macro-env {:column 1, :line 14, :ns {:defs .... }}]
[:if-cljs :clj] ;;; WRONG!
[:if-compiling-cljs :cljs] ;;; CORRECT
Note, [:if-cljs-env ...] is printed before [:if-cljs ...] as the if-cljs
macro expands inside the other-macro definition before other-macro is
executed from cljs.
My question is, why the &env in the if-cljs macro call here is not a
ClojureScript one? Bug or feature? :)
Saludos,
Nahuel Greco.
On Tue, Jun 17, 2014 at 11:01 PM, Jason Wolfe <[email protected]> wrote:
> Not anymore (thanks Chas):
>
> https://github.com/Prismatic/schema/pull/113
>
> On Tuesday, June 3, 2014 7:21:35 AM UTC-7, Nahuel Greco wrote:
> > check what Prismatic schema uses:
> https://github.com/Prismatic/schema/blob/master/src/clj/schema/macros.clj#L13-20
> >
> >
> >
> >
> >
> > Saludos,
> > Nahuel Greco.
> >
> >
> >
> > On Tue, Jun 3, 2014 at 9:40 AM, Ambrose Bonnaire-Sergeant <
> [email protected]> wrote:
> >
> >
> >
> >
> >
> > I would write two different macros.
> >
> >
> > Thanks,
> > Ambrose
> >
> >
> >
> >
> >
> >
> > On Tue, Jun 3, 2014 at 8:07 PM, Максим Карандашов <[email protected]>
> wrote:
> >
> >
> >
> >
> >
> >
> > I want to find (in the macro call) what happens: Clojure or
> ClojureScript compilation. It needed for using one name of macro inside
> Clojure and ClojureScript.
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > Currently I use next approach:
> >
> >
> >
> > (if (nil? cljs.env/*compiler*)
> >
> > (do-something-for-clojure)
> >
> > (do-something-for-clojurescript))
> >
> >
> >
> > And it's working well. But maybe this is not correct? Or is there
> another way to do this?
> >
> >
> >
> > --
> >
> > 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 http://groups.google.com/group/clojurescript.
> >
> >
> >
> >
> >
> >
> >
> > --
> >
> > 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 http://groups.google.com/group/clojurescript.
>
> --
> 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 http://groups.google.com/group/clojurescript.
>
--
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 http://groups.google.com/group/clojurescript.