Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: what is difference between resolve and var?

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
like image 706
Sonicsmooth Avatar asked Aug 09 '12 16:08

Sonicsmooth


1 Answers

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
like image 80
Justin Kramer Avatar answered Sep 30 '22 08:09

Justin Kramer