16 Vars and namespaces

 

Before introducing the functions and macros in this section, it’s useful to refresh a few definitions about Clojure namespaces, vars and local bindings.

Namespaces

  • A "namespace" is an instance of clojure.lang.Namespace class and is essentially a container for other objects. It contains a mapping table and an alias table.
  • The "mapping table" is a dictionary associated with each namespace. It contains a mapping between Clojure symbols and objects such as vars or classes. Functions like intern or def adds items into the table, while ns-map shows its content.
  • The "alias table" is another type of dictionary associated with a namespace. It contains relationships with other namespaces, using a symbol as key and a namespace name as value. Items can be added to the alias table with :as during namespace creation or with the alias function. ns-aliases shows its content.
  • Once a namespace is created, a reference is added to the "global namespace repository". This is a static map inside the clojure.lang.Namespace class itself. It derives that namespaces are "global", that is, the running process doesn’t need to hold an explicit namespace reference to keep them alive (from the garbage collector perspective).

Libraries

16.1 def, declare, intern and defonce

16.2 var, find-var and resolve

16.3 alter-var-root and with-redefs

16.4 binding

16.5 with-local-vars, var-get and var-set

16.6 ns, in-ns, create-ns and remove-ns

16.7 alias, ns-aliases and ns-unalias

16.8 ns-map and ns-unmap

16.9 ns-publics, ns-interns, ns-imports

16.10 refer, refer-clojure, require, loaded-libs, use, import

16.11 find-ns and all-ns

16.12 the-ns, ns-name and namespace

16.13 meta, with-meta, vary-meta, alter-meta! and reset-meta!