2

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?

1 Answer 1

3

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.

2
  • So is it shorthand for :my-current-namespace/keyword ? Sep 14, 2013 at 0:50
  • Yes. The current namespace will be used to namespace the keyword.
    – Jared314
    Sep 14, 2013 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.