Calling conform from within a macro

81 views
Skip to first unread message

Stanislav Yurin

unread,
Nov 22, 2017, 2:08:22 AM11/22/17
to Clojure
Hi,
Please help me understand following behavior.
Speccing macro via fdef and calling explain within a macro works, but conform tries to resolve all symbols.
How to correctly conform macro body from within a macro?

Longer example:

This works:

(spec/def ::test-spec (spec/* any?))

(spec/fdef test-macro
  :args ::test-spec
  :ret any?)

(defmacro test-macro [& body]
  (println body))

(test-macro a b c)
(a b c)
=> nil




This works:

(defmacro test-macro [& body]
 
(spec/explain ::test-spec body))


(test-macro a b c)
Success!
=> nil

This doesn't:


(defmacro test-macro [& body]
 
(spec/conform ::test-spec body))



(test-macro a b c)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: a in this context




Stanislav.

Alex Miller

unread,
Nov 22, 2017, 8:59:38 AM11/22/17
to Clojure
Your example does work, but it returns the conformed value [a b c] out of the macro, which the compiler then tries to evaluate. So the exception is coming downstream.

See:

user=> (macroexpand '(test-macro a b c))
[a b c]

Stanislav Yurin

unread,
Nov 22, 2017, 9:32:37 AM11/22/17
to clo...@googlegroups.com
Thanks a million!

--
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 a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/ixISA42ucVE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

S.
Reply all
Reply to author
Forward
0 new messages