Clojure: what's the difference between solution and var?

What is the difference between (resolve...)and (var...)? They both take a character and return var in the namespace. It looks like the solution is a function that takes the quote syntax as an argument, and var is a special form that takes the literal character typed in repl, but I don't understand how they will be used in different ways.

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
+5
source share
1 answer

resolvelooks at var (or class) with the character in mind and runs at runtime. varjust returns var and works at compile time. (var foo)is synonymous#'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
+9
source

All Articles