Vars, Bindings, and Namespaces

When you define an object with def or defn, that object is stored in a Clojure var. For example, the following def creates a var named user/foo:

 ​(​def​ foo 10)​
 ​-> #'user/foo​

The symbol user/foo refers to a var that is bound to the value 10. If you ask Clojure to evaluate the symbol foo, it will return the value of the associated var:

 ​foo​
 ​-> 10​

The initial value of a var is called its root binding. Sometimes it is useful to have thread-local bindings for a var; this is covered in Managing Per-Thread State with Vars.

You can refer to a var directly. The var special form returns a var itself, not the var’s value:

 ​(var a-symbol)​

You can use var to return the var bound to user/foo:

 ​(var foo)​
 ​-> ...

Get Programming Clojure, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.