syntax | since v0.0-1853 | in clojure | Edit |
Get the value that a reference is currently referring to. A "reference" can
be a atom
, delay
, or var
.
@foo
- (deref foo)
@foo
is sugar for (deref foo)
.
Quote the form to see how the reader expands it:
'@foo
;;=> (clojure.core/deref foo)
An atom
is a mutable reference to an immutable value.
(def a (atom 1))
@a
;;=> 1
(reset! a 2)
@a
;;=> 2
A delay
will evaluate by executing the given expression
the first time it is dereferenced.
(def a (delay (do (println "delayed.") 1)))
@a
;; prints:
;; delayed.
;;=> 1
@a
;;=> 1
Evaluating a symbol naturally dereferences the value of a var
.
But a var reference can be dereferenced as a roundabout way to obtain its value.
(Var references are mainly used to retrieve metadata on that var).
(def ^{:doc "my var"} a 1)
(:doc (meta #'a))
;;=> "my var"
(deref (var a))
;;=> 1
;; combining shorthand forms instead
@#'a
;;=> 1
(defn- macros [ch]
(case ch
\" read-string*
\: read-keyword
\; read-comment
\' (wrapping-reader 'quote)
\@ (wrapping-reader 'clojure.core/deref)
\^ read-meta
\` read-syntax-quote ;;(wrapping-reader 'syntax-quote)
\~ read-unquote
\( read-list
\) read-unmatched-delimiter
\[ read-vector
\] read-unmatched-delimiter
\{ read-map
\} read-unmatched-delimiter
\\ read-char*
\% read-arg
\# read-dispatch
nil))