mangolas
mangolas

Reputation: 165

Lein test failing on unbound var when using declare

Why is the last assertion from the following test failing when run by using lein test?

I don't understand why the first two assertions get the late bound value properly, but the fromvar value is seen as unbound.

(declare x)

(def fromvar x)
(defn fromfun [] x)

(deftest declaretest

  ;; These pass fine
  (is (= 1 x))
  (is (= 1 (fromfun)))

  ;; This fails on lein test:
  ;; expected: (= 1 fromvar)
  ;;   actual: (not (= 1 #<Unbound Unbound: #'my-test/value>))
  (is (= 1 fromvar)))

(def x 1)

Upvotes: 0

Views: 49

Answers (1)

noisesmith
noisesmith

Reputation: 20194

def doesn't ensure that fromvar is tied to the binding of value. It assigns to fromvar the value of x at the time def was called. Since x is not defined yet, the value is unbound (literally an instance of the inner class Var.Unbound). fromfun does not inline the value of x when compiled, so it is free to find the correct value at runtime, reflecting the later assignment.

Upvotes: 1

Related Questions