demi
demi

Reputation: 5504

Clojure: Meaning of ::tag

Recently I met construction like {::tag 10} in Clojure. What does it mean? My experiments showed up that it's a keyword to:

=> (type :tag)
clojure.lang.Keyword
=> (type ::tag)
clojure.lang.Keyword

The difference is value itself:

=> :tag
:tag
=> ::tag
:/user/tag

Seems like ::tag is namespace qualified. Is it right guess? If yes, what the difference between namespace qualified keyword and non-qualified? When it can be useful?

Upvotes: 2

Views: 176

Answers (1)

Jared314
Jared314

Reputation: 5231

The double-colon is the shorthand syntax for a namespace qualified keyword, with the current namespace. It is ideal for preventing collisions with external keyword based keys in hash-maps, such as having a :name and a ::name key in function metadata or comparing the :name vs ::name value of a parameter.

Just like you would namespace a public function to both identify it and prevent name conflicts, when exposing a custom keyword as a key or a value, that could conflict would existing keywords, you should consider namespace qualifying it.

Upvotes: 3

Related Questions