5
votes

What is the difference between (resolve...) and (var...)? They both take a symbol and return the var in the namespace. Looks like resolve is a function which takes the quote syntax as the argument and var is a special form which takes the literal symbol typed at the repl, but I don't understand how these would be used differently.

user> (def my-symbol 2.71828182846)
#'user/my-symbol
user> (resolve 'my-symbol)
#'user/my-symbol
user> (type (resolve 'my-symbol))
clojure.lang.Var
user> (var my-symbol)
#'user/my-symbol
user> (type (var my-symbol))
clojure.lang.Var
user> (= (resolve 'my-symbol) (var my-symbol))
true
1

1 Answers

10
votes

resolve looks up a var (or class) given a symbol, and operates at runtime. var just returns a var and operates at compile-time. (var foo) is synonymous with #'foo

(def foo "bar")
=> #'user/foo

(let [sym 'foo]
  (resolve sym))
=> #'user/foo

(let [sym 'foo]
  (var sym)) ;same as typing #'sym - doesn't actually refer to the sym local
=> Exception: Unable to resolve var: sym in this context