You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/reference/vars.adoc
+22-1Lines changed: 22 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,6 +93,27 @@ The <<special_forms#var,var>> special form or the `pass:[#']` reader macro _(see
93
93
94
94
It is possible to create vars that are not interned by using https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/with-local-vars[with-local-vars]. These vars will not be found during free symbol resolution, and their values have to be accessed manually. But they can serve as useful thread-local mutable cells.
95
95
96
+
[[metadata]]
97
+
== Var metadata
98
+
99
+
The forms that create vars `def`, `defn`, `defmacro`, etc use a standard set of var <<metadata#,metadata>> to describe vars. Some of these forms provide explicit places in the syntax to provide values stored in the metadata, but generally you can also supply that metadata as a map on the var symbol.
100
+
101
+
Common var metadata keys (all optional at var definition):
102
+
103
+
* `:doc` - a string documenting the var, usually set by the docstring parameter
104
+
* `:added` - a string documenting the version when this var was added
105
+
* `:private` a boolean flag, often set by `defn-`, used by the author to state the intent that this var is an implementation detail. Private vars are globally accessible but will not be referred or listed in `ns-`... functions which filter to non-private vars.
106
+
* `:arglists` - a coll of arglists, will be generated automatically if not supplied, most often used to document macro syntax
107
+
* `:macro` - a boolean flag added by `defmacro` automatically (not typically used directly)
108
+
* `:tag` - a type identifier (usually a class) for the type of the value in the var or the return type of a function held in the var. Note that var metadata is evaluated, so type hints like `^long` on the var will evaluate to the `long` function, not a `long` primitive type hint. Generally, it is preferred to use a type hint on the arglist for defn vars instead.
109
+
* `:test` - the `clojure.test` framework attaches unit tests to vars using this key (not typically used directly)
110
+
* `:dynamic` - indicates a var may be dynamically rebound in a thread context (see above). Dynamic vars will not be direct linked when compiling with direct linking.
111
+
* `:redef` - indicates that a var should not be direct linked when compiling with direct linking (thus allowing it to be redefined)
112
+
* `:static` - no longer used (originally vars were dynamic by default, now they are static by default)
113
+
* `:const` - indicates that a var is a compile-time constant and the compiler can inline the value into code that uses it. Note: this is rarely needed and only works with constants at compile time (read, but not evaluated), such as numbers, strings, etc (NOT classes, functions, ref types, etc). Redefining or dynamically binding a const var will not affect code that consumes the var that has already been compiled and loaded in the runtime.
114
+
115
+
Also see <<compilation#_compiler_options,Compiler Options>> for more information about direct linking and metadata elision during compilation.
116
+
96
117
[[related]]
97
118
== Related functions
98
119
@@ -101,4 +122,4 @@ Variants of `def`: https://clojure.github.io/clojure/clojure.core-api.html#cloju
101
122
Working with interned Vars: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/declare[declare] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/intern[intern] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/binding[binding] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/find-var[find-var] <<special_forms#var,var>>
102
123
Working with Var objects: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/with-local-vars[with-local-vars] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/var-get[var-get] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/var-set[var-set] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/alter-var-root[alter-var-root] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/var?[var?] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/with-redefs[with-redefs] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/with-redefs-fn[with-redefs-fn]
103
124
Var validators: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/set-validator![set-validator!] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/get-validator[get-validator]
104
-
Common Var metadata: https://clojure.github.io/clojure/clojure.repl-api.html#clojure.repl/doc[doc] https://clojure.github.io/clojure/clojure.repl-api.html#clojure.repl/find-doc[find-doc] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/test[test]
125
+
Using Var metadata: https://clojure.github.io/clojure/clojure.repl-api.html#clojure.repl/doc[doc] https://clojure.github.io/clojure/clojure.repl-api.html#clojure.repl/find-doc[find-doc] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/test[test]
0 commit comments