Why does Clojure have "keywords" in addition to "symbols"?

26,086

Solution 1

Here's the Clojure documentation for Keywords and Symbols.

Keywords are symbolic identifiers that evaluate to themselves. They provide very fast equality tests...

Symbols are identifiers that are normally used to refer to something else. They can be used in program forms to refer to function parameters, let bindings, class names and global vars...

Keywords are generally used as lightweight "constant strings", e.g. for the keys of a hash-map or the dispatch values of a multimethod. Symbols are generally used to name variable and functions and it's less common to manipulate them as objects directly except in macros and such. But there's nothing stopping you from using a symbol everywhere you use a keyword (if you don't mind quoting them all the time).

The easiest way to see the difference is to read Keyword.java and Symbol.java in the Clojure source. There are a few obvious implementation differences. For example a Symbol in Clojure can have metadata and a Keyword can't.

In addition to single-colon syntax, you can use a double-colon to make a namespace-qualified keyword.

user> :foo
:foo
user> ::foo
:user/foo

Common Lisp has keywords, as do Ruby and other languages. They are slightly different in those languages of course. Some differences between Common Lisp keywords and Clojure keywords:

  1. Keywords in Clojure are not Symbols.

    user> (symbol? :foo)  
    false
    
  2. Keywords don't belong to any namespace unless you specifically qualify them:

    user> (namespace :foo)
    nil
    user> (namespace ::foo)
    "user"
    

(Thanks Rainer Joswig for giving me ideas of things to look at.)

Solution 2

Common Lisp has keyword symbols.

Keywords are symbols, too.

(symbolp ':foo) -> T

What makes keywords special:

  • :foo is parsed by the Common Lisp reader as the symbol keyword::foo
  • keywords evaluate to themselves: :foo -> :foo
  • the home package of keyword symbols is the KEYWORD package: keyword:foo -> :foo
  • keywords are exported from the package KEYWORD
  • keywords are constants, it is not allowed to assign a different value

Otherwise keywords are ordinary symbols. So keywords can name functions or have property lists.

Remember: in Common Lisp symbols belong to a package. This can be written as:

  • foo, when the symbol is accessible in the current package
  • foo:bar, when the symbol FOO is exported from the package BAR
  • foo::bar, when the symbol FOO is in the package BAR

For keyword symbols that means that :foo, keyword:foo and keyword::foo are all the same symbol. Thus the latter two notations are usually not used.

So :foo is just parsed to be in the package KEYWORD, assuming that giving no package name before the symbol name means by default the KEYWORD package.

Solution 3

Keywords are symbols that evaluate to themselves, so you don't have to remember to quote them.

Solution 4

:keywords are also treated specially by many of the collections, allowing for some really convenient syntax.

(:user-id (get-users-map))

is the same as

((get-users-map) :user-id)

this makes things just a little more flexable

Solution 5

For keywords, hash values are calculated and cached when the keyword is first constructed. When looking up a keyword as a hash key, it simply returns the precomputed hashed value. For strings and symbols, the hash is recalculated on every lookup.

The why same named keywords are always identical, they contains their own hash values. As search in maps and sets are made from hash keys this envolves better search efficiency in case of numerous searches, not in the search itself.

Share:
26,086
Laurence Gonsalves
Author by

Laurence Gonsalves

I'm the founder of Niphtio. If you'd like to work with me, we're hiring! When it comes to computing, I'm a bit of a generalist: I'm interested in programming language design, compilers, computer graphics, machine learning, robotics, games, and more. I regularly use Python, Kotlin, Java and C++, and am also a fan of Scheme. I like functional programming and static typing, but am not really a fan of Haskell... go figure. My editor of choice is Vim. I grew up with a Commodore 64, then a 128 and eventually had an Amiga. These days I primarily use Linux.

Updated on October 04, 2020

Comments

  • Laurence Gonsalves
    Laurence Gonsalves over 3 years

    I have a passing knowledge of other Lisps (particularly Scheme) from way back. Recently I've been reading about Clojure. I see that it has both "symbols" and "keywords". Symbols I'm familiar with, but not with keywords.

    Do other Lisps have keywords? How are keywords different from symbols other than having different notation (ie: colons)?

  • Laurence Gonsalves
    Laurence Gonsalves over 14 years
    Is that it? Typing : rather than ' doesn't seem like a big win, especially since : is an extra keypress on most keyboards.
  • Greg Hewgill
    Greg Hewgill over 14 years
    Well, it's more than just the character, really. Keywords stay keywords after evaluation, while symbols are evaluated to whatever they bind to. It's more like a semantic difference, because they're typically used for different purposes.
  • Jonas
    Jonas over 14 years
    This is also true for symbols, ('a {'a 1 'b 2}) => 1 and ({'a 1 'b 2} 'b) => 2.
  • Admin
    Admin over 14 years
    This explains what the differences are, but not why two different constructs are needed. Couldn't Clojure have created something with the union of the capabilities of both Keyword and Symbol?
  • Ben Richardson
    Ben Richardson over 14 years
    Keywords are lightweight and have a convenient syntax, I think that's about all there is to it. The language would work fine without them but they're nice to have and they are very widely used. You can't have a union of their abilities because Keywords are always self-evaluating (i.e. you can't use them as variable or function names) and Symbols in general can't be always self-evaluating.
  • David Plumpton
    David Plumpton over 13 years
    Keywords are not symbols in Clojure
  • kristianlm
    kristianlm over 11 years
    It seems keywords are more useful as keys in hashmaps etc as they don't change once evaluated: (eval (eval ':a)) vs (eval (eval ''a)). Are there other advantages? Performance wise, they are identical?
  • desudesudesu
    desudesudesu over 10 years
    (identical? :qwe :qwe) -> true. (identical? 'qwe 'qwe) -> false. Symbols use interned string inside, so comparison is fast too.
  • David J.
    David J. over 3 years
    Why do you use JavaScript examples here? It seems confusing to me.
  • John Leidegren
    John Leidegren almost 3 years
    @DavidJ. because I port a lot of Clojure code to JavaScript and it maps pretty well. You can also run it in your browser without any hassle. It was the most natural way for me to explain the concept. Also, it adds another dimension to the explanation. So if you happen to know JavaScript you can use that knowledge to map that onto Clojure as you learn more and more. JavaScript is also the most common programming language in the world. It isn't hurting anyone.