Creates and interns or locates a global var with the name of symbol and a namespace of the value of the current namespace ( *ns* ). If init is supplied, it is evaluated, and the root binding of the var is set to the resulting value. If init is not supplied, the root binding of the var is unaffected. def always applies to the root binding, even if the var is thread-bound at the point where def is called. def yields the var itself (not its value). Throws an exception if symbol is already in the namespace and not mapped to an interned var. Support for doc-string was added in Clojure 1.3.

Any metadata on the symbol will be evaluated, and become metadata on the var itself. There are several metadata keys that have special interpretation:

:private a boolean indicating the access control for the var. If this key is not present, the default access is public (e.g. as if :private false ).

:doc a string containing short (1-3 line) documentation for the var contents

:test a fn of no args that uses assert to check various operations. The var itself will be accessible during evaluation of a literal fn in the metadata map.

:tag a symbol naming a class or a Class object that indicates the Java type of the object in the var, or its return value if the object is a fn.

In addition the compiler will place the following metadata keys on the var:

:file string

:line int

:name simple symbol

:ns namespace in which var is interned

:macro true if var names a macro

:arglists a list of vector(s) of argument forms, as were supplied to defn

The var metadata can be used for application-specific purposes as well. Consider using namespace-qualified keys (e.g. :myns/foo ) to avoid clashes.

(defn ^{:doc "mymax [xs+] gets the maximum value in xs using > " :test (fn [] (assert (= 42 (mymax 2 42 5 4)))) :user/comment "this is the best fn ever!"} mymax ([x] x) ([x y] (if (> x y) x y)) ([x y & more] (reduce mymax (mymax x y) more))) user=> (meta #'mymax) {:name mymax, :user/comment "this is the best fn ever!", :doc "mymax [xs+] gets the maximum value in xs using > ", :arglists ([x] [x y] [x y & more]) :file "repl-1", :line 126, :ns #<Namespace user >, :test #<user$fn__289 user$fn__289@20f443 >}

Many macros expand into def (e.g. defn , defmacro ), and thus also convey metadata for the resulting var from the symbol used as the name.