Reputation: 63
I understand how to attach metadata such as:
(def x ^{:foo true} [1 2])
But a Clojure video on Youtube uses the example:
(def ^{:foo true} x [1 2])
(meta) doesn't return the metadata on the Youtube example.
What is it attaching the metadata to, why would you do this and how do I return the metadata? Thanks.
Upvotes: 3
Views: 202
Reputation: 22258
^
is a reader macro that "first reads the metadata and attaches it to the next form"
Attach metadata to the vector [1 2]
(def x ^{:foo true} [1 2])
(meta x) ;; x resolves to the vector, `meta` retrieves the metadata attached to it
Attach metadata to the var x
(def ^{:foo true} x [1 2])
(meta #'x) ;; retrieve the metadata from the var `x`
Consider this repl session where we attach metadata to the var and the value:
user=> (def ^{:var-foo true} x ^{:val-foo true} [1 2])
#'user/x
user=> (meta x)
{:val-foo true}
user=> (meta #'x)
{:ns #<Namespace user>, :name x, :file "NO_SOURCE_PATH", :var-foo true, :column 1, :line 1}
Upvotes: 4