What do you need to know about predefined keywords like: refer,: use and: only in Clojure?

advertisements

We use special keywords such as :refer, :as, :only and others, in namespacing.

What are those keywords called? The books I've seen so far simply refer to them as themselves, except for The Joy of Clojure which seems to call them "directives" on page 199.

Clojure.org calls them "metadata keys," under "special forms" when they relate to vars. Somehow those docs don't seem to be telling the whole story (or at least I'm not really getting it), and maybe I'm confusing a different kind of key? Vars have "metadata keys" like :doc and :name, whereas using :refer to control namespacing seems to do something. Are these both the same types?

I'm referring to words preceded by colons, like regular keywords, but which have special purposes in Clojure.

What should we know about them? Keywords? Directives, whatever that means? Metadata keywords?

And, if all vars have metadata keys, can we access them like we can with JavaScript vars, prototypes, etc., in a browser console, for example?

As you may have guessed, a Clojure beginner here... Thanks.


You have to distinguish what they are and what they mean.

Keywords are just symbolic identifiers that evaluate to themselves.

Depending on the context, they can have different purposes. You can define your own function where keywords mean something special for you. Clojure has different contexts where keywords serve a very specific role, as the one you write about in your question. The specific names metadata keys refer to a particular role attributed to a set of known keywords in a specific case.

Symbols and collections (not variables) can be bound to metadata (like symbol properties in CL). You can access this metadata simply by calling (meta s) for a symbol s. See the documentation for metadata.