Wednesday, March 19, 2014

var and symbol

A symbol object stands for the symbol itself, not the value.
Example:

'abc 
(symbol "abc")

These guys are the same thing. They are a symbol object.
In runtime, you can evaluate a symbol to find out the value associated with them.

;;; suppose you had a variable named "abc"
; (def abc 20)
(eval 'abc)
(eval (symbol "abc"))  ;; ==> 20

A var is an object holding the value of a symbol.

(def ^:dynamic a 3)
(binding [a 30]  
  (var-set (var a) 300) 
  (println a) 
  (var-set (resolve (symbol "a"))  3000) 
  (println a)
  (var-set #'a  30000) 
  (println a)
  (set! a 30000)   (println a) 
)

1. In compile time (var a) is resolved to the var object itself.
2. In runtime, (resolve 'a) or (resolve (symbol "a")) is something stands for the var object itself.


// #'xxx is a macro, expands to (var xxx)

No comments:

Post a Comment