George
George

Reputation: 7317

Prepending metadata to a var vs. a data structure in Clojure

What, effectively, is the difference between these two metadata declarations? Why would you use one over the other?

(def a0 ^{:answer-to-everything 42} {:language "ClojureScript"})

(def ^{:answer-to-everything 42} a1 {:language "ClojureScript"})

I take it that in the first case the metadata is being prepended to the map, while in the second case the metadata is being prepended to the var. Assuming I am correct, I still don't understand why you would ever prefer one over the other.

Upvotes: 2

Views: 58

Answers (1)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91554

in cases where you want to have the metadata follow the value as it's passed from function to function then use the first case. And when you don't use the second:

user> (def a0 ^{:answer-to-everything 42} {:language "ClojureScript"})
#'user/a0
user> (def ^{:answer-to-everything 42} a1 {:language "ClojureScript"})
#'user/a1
user> (print-the-metadata-from-a-function a0)
{:answer-to-everything 42}
nil
user> (print-the-metadata-from-a-function a1)
nil
nil
user> (print-the-metadata-from-a-function #'a1)
{:answer-to-everything 42, :line 74, :column 6, :file *cider-repl api*, :name a1, :ns #namespace[user]}
nil

Upvotes: 2

Related Questions