Reputation: 8096
Getting unresolved on dynamic var when called from n-levels out from originating namespace
Setup:
File a.clj
-----------
(ns a)
(def ^:dynamic *poof* "poof")
(def x [1 '*poof* 3])
(defn- func-wbind [new-term]
(binding [*poof* new-term]
(println (eval x))))
(defn with-term [term]
(println x)
(println (eval x)) ; <== *** FAILS HERE ***
(func-wbind term))
File b.clj
-----------
(ns b
(:require [a :refer :all]))
(defn woof-it []
(with-term "woof"))
File c.clj
-----------
(ns c
(:require [b :refer :all]))
(defn try-it []
(woof-it))
Loading REPL with 'c' and calling try-it fails on unresolved var for the dynamic.
Any help would be greatly appreciated.
Edit : Above modified and repeatable error.
Stack Trace:
ERROR in (endpoint-tests) (Compiler.java:6380)
Default
expected: (map? (endpoint-abstractions ep-any "www.yahoo.com"))
actual: clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve symbol: *ph* in this context, compiling:(gl_ep/epref_tests.clj:17:1)
at clojure.lang.Compiler.analyze (Compiler.java:6380)
clojure.lang.Compiler.analyze (Compiler.java:6322)
clojure.lang.Compiler$HostExpr$Parser.parse (Compiler.java:948)
clojure.lang.Compiler.analyzeSeq (Compiler.java:6560)
clojure.lang.Compiler.analyze (Compiler.java:6361)
clojure.lang.Compiler.analyze (Compiler.java:6322)
clojure.lang.Compiler.analyzeSeq (Compiler.java:6555)
clojure.lang.Compiler.analyze (Compiler.java:6361)
clojure.lang.Compiler.analyze (Compiler.java:6322)
clojure.lang.Compiler$BodyExpr$Parser.parse (Compiler.java:5708)
clojure.lang.Compiler$FnMethod.parse (Compiler.java:5139)
clojure.lang.Compiler$FnExpr.parse (Compiler.java:3751)
clojure.lang.Compiler.analyzeSeq (Compiler.java:6558)
clojure.lang.Compiler.analyze (Compiler.java:6361)
clojure.lang.Compiler.eval (Compiler.java:6616)
clojure.lang.Compiler.eval (Compiler.java:6582)
clojure.core$eval.invoke (core.clj:2852)
gl_ep.html_extract$glg_hfm_transform$fn__5320$fn__5321.invoke (html_extract.clj:35)
hickory.select$attr$fn__5193.invoke (select.clj:220)
clojure.lang.AFn.applyToHelper (AFn.java:161)
clojure.lang.AFn.applyTo (AFn.java:151)
clojure.lang.AFunction$1.doInvoke (AFunction.java:29)
clojure.lang.RestFn.invoke (RestFn.java:408)
hickory.select$ordered_adjacent$fn__5271.invoke (select.clj:481)
hickory.select$select_next_loc.invoke (select.clj:134)
hickory.select$select_next_loc.invoke (select.clj:129)
hickory.select$select_next_loc.invoke (select.clj:127)
hickory.select$select_locs.invoke (select.clj:143)
hickory.select$select.invoke (select.clj:154)
gl_ep.html_extract$hickory_get.invoke (html_extract.clj:66)
gl_ep.html_extract$mapglg$fn__5363.invoke (html_extract.clj:76)
clojure.core$map$fn__4207.invoke (core.clj:2485)
clojure.lang.LazySeq.sval (LazySeq.java:42)
clojure.lang.LazySeq.seq (LazySeq.java:60)
clojure.lang.RT.seq (RT.java:484)
clojure.core$seq.invoke (core.clj:133)
clojure.core$map$fn__4207.invoke (core.clj:2479)
clojure.lang.LazySeq.sval (LazySeq.java:42)
clojure.lang.LazySeq.seq (LazySeq.java:60)
clojure.lang.RT.seq (RT.java:484)
clojure.core$seq.invoke (core.clj:133)
clojure.core$apply.invoke (core.clj:617)
gl_ep.html_extract$mapglg.invoke (html_extract.clj:73)
gl_ep.html_extract$extract.invoke (html_extract.clj:87)
gl_ep.endpoint$abstractions.doInvoke (endpoint.clj:59)
clojure.lang.RestFn.invoke (RestFn.java:423)
gl_ep.endpoint$endpoint_abstractions.doInvoke (endpoint.clj:68)
clojure.lang.RestFn.invoke (RestFn.java:423)
gl_ep.epref_tests$fn__8816$fn__8817.invoke (epref_tests.clj:10)
gl_ep.epref_tests/fn (epref_tests.clj:10)
clojure.test$test_var$fn__7145.invoke (test.clj:701)
clojure.test$test_var.invoke (test.clj:701)
clojure.test$test_all_vars$fn__7149$fn__7156.invoke (test.clj:717)
clojure.test$default_fixture.invoke (test.clj:671)
clojure.test$test_all_vars$fn__7149.invoke (test.clj:717)
clojure.test$default_fixture.invoke (test.clj:671)
clojure.test$test_all_vars.invoke (test.clj:713)
clojure.test$test_ns.invoke (test.clj:736)
clojure.core$map$fn__4207.invoke (core.clj:2487)
clojure.lang.LazySeq.sval (LazySeq.java:42)
clojure.lang.LazySeq.seq (LazySeq.java:60)
clojure.lang.Cons.next (Cons.java:39)
clojure.lang.RT.boundedLength (RT.java:1654)
clojure.lang.RestFn.applyTo (RestFn.java:130)
clojure.core$apply.invoke (core.clj:619)
clojure.test$run_tests.doInvoke (test.clj:751)
clojure.lang.RestFn.invoke (RestFn.java:408)
clojure.test$run_tests.invoke (test.clj:749)
Upvotes: 2
Views: 450
Reputation: 13961
When you eval
a form, unqualified symbols in that form are resolved in the current namespace (as defined by clojure.core/*ns*
) at runtime, not the namespace in which the call to eval
was compiled. So the symbol *poof*
is resolved in the REPL namespace. This works in namespace b
because you did a :refer :all
when requiring a
, thereby creating a local alias to a/*poof*
in b
. There exists no such alias in c
, and so resolving the symbol fails.
There are a couple of options to fix this. You could change the declaration of x
to use a namespace-qualified symbol:
(def x [1 'a/*poof* 3])`
Or, you could make the call to eval
in a binding of *ns*
to namespace a
.
(def x [1 '*poof* 3])
(defn- local-eval [x]
(binding [*ns* (find-ns 'a)]
(eval x)))
and then replace calls to eval
with calls to local-eval
.
See the Clojure documentation for a more detailed explanation of how symbols are resolved during evaluation.
Upvotes: 4