2
votes

http://clojure.org/metadata says "Symbols and collections support metadata"

So I try to set metadata to a symbol:

=> a 
17 

=> (def aa ^a 'x) 

=> aa 
x 

=> (meta aa) 
nil 

It does not work as I expected.

=> (def aa ^a []) 

=> (meta aa) 
{:tag 17} 

This does.

Is this a mistake in the documentation? If not, can you please explain?

Update after answer by Arthur Ulfeldt: So I understand it as follows. When I wrote

(def aa ^a 'x)

the reader expanded it into

(def aa ^a (quote x))

so the metadata were at the list (quote x), and not at the symbol. When evaluating the def macro, this list got evaluated, leaving us with x, and the metadata were lost.

1

1 Answers

4
votes

It works if instead of using the quote reader macro you write out the (quote x) expression and then attach the metadata to the symbol within the quote:

user> (def aa (quote ^unevaluated-symbol x))
#'user/aa

user> (meta aa)
{:tag unevaluated-symbol}

It's worth noting that when you put the symbol with the quote it never gets a chance to be evaluated. if you want it evaluated you can skip the whole quoting and generate the symbol with the symbol function:

user> (def aa  (with-meta (symbol "x") {:foo a}))
#'user/aa
user> (meta aa)
{:foo 17}