[CLJ-1276] Can't make a dispatch map containing forward-declared fns Created: 09/Oct/13 Updated: 18/Apr/14 Resolved: 18/Apr/14
|Affects Version/s:||Release 1.6|
If from (ns tst2) you try to call tst1/c, which calls tst1/f via dispatch map which was defined when tst1/f was forward declared, you get an "unbound fn" error. E.g.
|Comment by Alex Coventry [ 09/Oct/13 10:43 PM ]|
TEttinger pointed out on IRC that the forms in the example run without error if you wrap them in a (do) block. Here is an example using files. Relevant code is in src/unbound_eg/tst.clj. Example output shown below.
lap% lein repl
user=> (require '[unbound-eg.tst2 :as t2])
IllegalStateException Attempting to call unbound fn: #'unbound-eg.tst1/f clojure.lang.Var$Unbound.throwArity (Var.java:43)
|Comment by Kevin Downey [ 18/Apr/14 12:23 AM ]|
this is just a fact of clojure's compilation model and how vars work.
a var is a little mutable cell
(declare foo) declares that a mutable cell exists with the name foo, it doesn't contain a value
foo then gets the value of the mutable cell (which has none)
(defn foo  1) then sets the value of the cell named foo to the function created from (fn  1)
|Comment by Alex Miller [ 18/Apr/14 7:29 AM ]|
I agree with Kevin - this is expected behavior.