I have wondered how clojurescript compiler processes macros.
So I tested as following:

I made dirs and files as below.

guruma@mac cljs $ tree .
.
├── build.clj
├── cljs.jar
└── src
    └── foo
        ├── core.cljs
        └── macro.cljc

2 directories, 4 files

I downloaded cljs.jar using curl.

guruma@mac cljs $ curl -LOk 
https://github.com/clojure/clojurescript/releases/download/r1.9.908/cljs.jar

And I made build.clj, core.cljs and macro.cljc as below.

build.clj
(require 'cljs.build.api)

(cljs.build.api/build
  "./src"
  {:main 'core
   :output-to "out/main.js"})

core.cljs
(ns foo.core
  (:require-macros [macro :refer [log]]))

(enable-console-print!)

(log "hello, world")

core.cljs require macro.cljc and use a macro 'log'.

macro.cljc
(ns foo.macro)

(defn now []
  #?(:clj (System/currentTimeMillis)
     :cljs (js/Date.now)))

(defmacro log [x]
  `(println "log[" (now) "]: " ~x))

macro.cljc defines a macro 'log'.
And there is the helper function 'now' that has read-conditionals in its 
body. 

And compile.

guruma@mac cljs $ java -cp cljs.jar:src clojure.main build.clj
guruma@mac cljs $

No error and no warning.

And I modified macro.cljc as below

macro.cljc  modified
(ns foo.macro)

#?(:clj (defn now []  ; <== modified
    (System/currentTimeMillis)))

(defmacro log [x]
  `(println "log[" (now) "]: " ~x))

the function 'now' is in read-conditional.

And compile.

guruma@mac cljs $ java -cp cljs.jar:src clojure.main build.clj
WARNING: Use of undeclared Var foo.macro/now at line 6 ./src/foo/core.cljs

Here is a warning and it is out of question because there is no definition 
of the function 'now' for clojurescript to be used for runtime.

But what i can't understand is why there is no warning when compiling 
unmodified macro.cljc.

My questions are:

1) Does clojurescript compiler load macro twice for :clj and :cljs in 
read-conditional when handling :require-macros?
2) How does clojurescript compiler resolve the symbol 'now' defined in 
macro.cljc?

Thanks a lot in advance.










-- 
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 clojurescript+unsubscr...@googlegroups.com.
To post to this group, send email to clojurescript@googlegroups.com.
Visit this group at https://groups.google.com/group/clojurescript.

Reply via email to