Strange eval behavior on user-defined macros

16 views
Skip to first unread message

Chris McBride

unread,
Dec 25, 2011, 1:22:00 AM12/25/11
to Clojure
I'm working on a project where I want to use clojure macros in part of
my config file that I eval at runtime. However, I'm getting an "Unable
to resolve symbol" exception. So I made a test case to reproduce it
below:

(ns test.core (:gen-class))

(defmacro make-list [& body]
`(list ~@body))

(defn stuff[]
(prn (eval (read-string "(make-list 1 2 3)"))))

(defn -main [& args] (stuff))
(-main)

When I run this file through the clojure interpreter I get the output
"(1 2 3)" from the -main call. Now if I do 'lein run' with my 'main'
namespace defined as test.core, I get this output:

(1 2 3)
Exception in thread "main" java.lang.RuntimeException: Unable to
resolve symbol: make-list in this context, compiling:(NO_SOURCE_PATH:
1)
at clojure.lang.Compiler.analyze(Compiler.java:6235)
at clojure.lang.Compiler.analyze(Compiler.java:6177)
at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3452)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6411)
at clojure.lang.Compiler.analyze(Compiler.java:6216)
at clojure.lang.Compiler.analyze(Compiler.java:6177)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5572)
at clojure.lang.Compiler$FnMethod.parse(Compiler.java:5008)
at clojure.lang.Compiler$FnExpr.parse(Compiler.java:3629)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6407)
at clojure.lang.Compiler.analyze(Compiler.java:6216)
at clojure.lang.Compiler.eval(Compiler.java:6462)
at clojure.lang.Compiler.eval(Compiler.java:6431)
at clojure.core$eval.invoke(core.clj:2795)
at test.core$stuff.invoke(core.clj:7)
at test.core$_main.doInvoke(core.clj:9)
at clojure.lang.RestFn.invoke(RestFn.java:397)
at clojure.lang.Var.invoke(Var.java:397)
at user$eval46.invoke(NO_SOURCE_FILE:1)
at clojure.lang.Compiler.eval(Compiler.java:6465)
at clojure.lang.Compiler.eval(Compiler.java:6455)
at clojure.lang.Compiler.eval(Compiler.java:6431)
at clojure.core$eval.invoke(core.clj:2795)
at clojure.main$eval_opt.invoke(main.clj:296)
at clojure.main$initialize.invoke(main.clj:315)
at clojure.main$null_opt.invoke(main.clj:348)
at clojure.main$main.doInvoke(main.clj:426)
at clojure.lang.RestFn.invoke(RestFn.java:421)
at clojure.lang.Var.invoke(Var.java:405)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.main.main(main.java:37)
Caused by: java.lang.RuntimeException: Unable to resolve symbol: make-
list in this context
at clojure.lang.Util.runtimeException(Util.java:156)
at clojure.lang.Compiler.resolveIn(Compiler.java:6720)
at clojure.lang.Compiler.resolve(Compiler.java:6664)
at clojure.lang.Compiler.analyzeSymbol(Compiler.java:6625)
at clojure.lang.Compiler.analyze(Compiler.java:6198)
... 31 more

So first I get the "(1 2 3)" output from clojure calling -main when
it reads the file, but it seems like when lein calls -main it can't
resolve the symbol. Is this a limitation of java since this is at
runtime and macros have to be expanded at read-time? Or is this a bug?

Meikel Brandmeyer

unread,
Dec 26, 2011, 5:37:32 PM12/26/11
to clo...@googlegroups.com
Hi,

eval happens with *ns* bound to clojure-core. Try to fully qualify make-list.

Sincerely
Meikel

PS: In case you are Chris on SO, I answered your question there also.

Reply all
Reply to author
Forward
0 new messages