Help with macro.

72 views
Skip to first unread message

Andrey Antukh

unread,
Oct 3, 2015, 9:03:49 AM10/3/15
to clo...@googlegroups.com
Hi!

I have a little trouble writing a macro because I'm getting unexpected (for me) behavior.

Let see some code:

(ns foo.bar)

(defn debug
  [x]
  (println "debug:" x)
  x)

(defn debug-expr?
  [expr]
  (and (seq? expr)
       (symbol? (first expr))
       (= 'foo.bar/debug  (first expr))))

(defmacro without-debug
  [& body]
  (let [body' (reduce (fn [acc v]
                        (if (debug-expr? v)
                          (conj acc (second v))
                          (conj acc v)))
                      [] body)]
    `(do
       ~@body')))


And then I use it from other namespace:

(ns foo.baz)

(require '[foo.bar :as b])

(macroexpand '(b/without-debug (b/debug 3)))
;; => (do (b/debug 3))

(macroexpand '(b/without-debug (foo.bar/debug 3)))
;; =>(do 3)
  
I expect that the both expressions will evaluate to the same result, but is not. Seems that symbols inside macros are not fully qualified. It there any way to get them fully qualified? I'm missing something?

Any help is welcome!

Thank you very much.

Andrey
--
Andrey Antukh - Андрей Антух - <ni...@niwi.nz>

gianluca torta

unread,
Oct 3, 2015, 10:24:56 AM10/3/15
to Clojure
Hi,

the behavior you describe is not specific to macros, but is due to the use of aliases

after:

(require '[foo.bar :as b])

this will give you false:
(= 'foo.bar/x (first '(b/x)))

while this will give you true:
(= 'foo.bar/x (first '(foo.bar/x)))

one way to solve it, is comparing the resolved vars instead of the names:
(= (resolve 'foo.bar/x) (resolve (first '(b/x))))
;=>true
(= (resolve 'foo.bar/x) (resolve (first '(foo.bar/x))))
;=>true

hth,
Gianluca

Andrey Antukh

unread,
Oct 3, 2015, 10:32:37 AM10/3/15
to clo...@googlegroups.com
On Sat, Oct 3, 2015 at 5:24 PM, gianluca torta <giat...@gmail.com> wrote:
Hi,

the behavior you describe is not specific to macros, but is due to the use of aliases

after:
(require '[foo.bar :as b])

this will give you false:
(= 'foo.bar/x (first '(b/x)))

while this will give you true:
(= 'foo.bar/x (first '(foo.bar/x)))

one way to solve it, is comparing the resolved vars instead of the names:
(= (resolve 'foo.bar/x) (resolve (first '(b/x))))
;=>true
(= (resolve 'foo.bar/x) (resolve (first '(foo.bar/x))))
;=>true


Yes, your solution works, but only on clojure. ClojureScript doesn't have `resolve`. It there any portable solution?

Thanks!

Andrey
 
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gianluca torta

unread,
Oct 4, 2015, 9:46:08 AM10/4/15
to Clojure


Yes, your solution works, but only on clojure. ClojureScript doesn't have `resolve`. It there any portable solution?


I'm not sure if and how you can do it in ClojureScript... have you tried:
https://groups.google.com/forum/#!forum/clojurescript

cheers,
Gianluca
Reply all
Reply to author
Forward
0 new messages