name | display as | clojure doc | see also | |||||
---|---|---|---|---|---|---|---|---|
syntax/meta |
^ meta |
|
Attach metadata to the following symbol or collection. Use a map to specify the metadata, or use shorthand:
^:foo
=>^{:foo true}
^"foo"
=>^{:tag "foo"}
^foo
=>^{:tag foo}
Attach metadata to a collection:
^:foo [1 2 3]
;;=> [1 2 3]
View the resulting metadata:
(meta ^:foo [1 2 3])
;;=> {:foo true}
(meta ^{:foo "bar"} [1 2 3])
;;=> {:foo "bar"}
(meta ^"foo" [1 2 3])
;;=> {:tag "foo"}
(def foo 1)
(meta ^foo [1 2 3])
;;=> {:tag 1}
Chain metadata:
(meta ^:foo ^"foo" [1 2 3])
;;=> {:foo true, :tag "foo"}
Generally, metadata maps used purely by the compiler are not evaluated and should be read literally:
(defn foo [^js x] ;; ^js => ^{:tag js} (literal map not evaluated)
^js (.-bar x)) ;; ^js => ^{:tag js} (literal map not evaluated)
But here’s one exception— metadata maps are evaluated when placed behind literal collections created at runtime:
(def foo (+ 1 2 3))
(meta ^foo {}) ;;=> {:tag 6} not {:tag foo}
(meta ^{:foo (+ 1 2 3)} []) ;;=> {:foo 6} not {:foo (+ 1 2 3)}