Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in Clojure, why have Strings, Keywords AND Symbols?

Tags:

I'm in the process of learning Clojure and I can't understand some language design decisions: Why does a language with immutable Strings like Clojure also needs Keywords and Symbols data types? Couldn't strings just have optional namespaces and metadata and all this stuff? For immutable strings comparison could just as well be identity base, no?

Or, since interop with Java is a must have for Clojure, at least have the Java String type and a KeywordSymbol data type.

I find this String/Keyword/Symbol "trichotomy" especially weird since Clojure seems very focused on "purity" and keeping things simple in other aspects.

like image 523
NeuronQ Avatar asked Jul 25 '12 17:07

NeuronQ


People also ask

What are symbols in Clojure?

In Common Lisp, a "symbol" is a location in memory, a place where data can be stored. The "value" of a symbol is the data stored at that location in memory. In Clojure, a "symbol" is just a name. It has no value.

What are keywords in Clojure?

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...


1 Answers

They fill very different roles within the language:

  • Vars are used to give names to things. They implement runnable and can be used directly to invoke functions. You cannot run a string.
  • Keywords are names by themselves, and look themselves up in maps. They really help Clojure keep its "data driven" flavor. Strings do not implement the required interfaces to look themselves up in maps.
  • Strings are just strings. They do what they need to do and not much more.

One of the core principles in the design of Clojure was to embrace your host platform, thus in Clojure strings are Java strings and you never need to wrap a Java string in some convert-to-clojure-string function in order to get it into the Clojure ecosystem. This necessitated using unmodified Java strings, as well as the numeric types. Keywords and symbols are new constructs that are being added by Clojure, so it is only necessary to make them accessible in a useful way from the rest of the Java ecosystem. Symbols and Keywords make themselves accessible by simply being classes that implement an interface. It was believed in the beginning that in order for a new language to succeed in the JVM ecosystem, it needed to fully embrace Java and minimise the "impedance mismatch" (sorry for the buzzwordism) even if that required adding more to the language than would have been required without this goal.

edit:


You can sort of turn a symbol into a keyword by defing it to it's self

user> a ; Evaluation aborted. user> :a :a user> (def a 'a) #'user/a user> a a user>  

keywords evaluate to themselves

like image 61
Arthur Ulfeldt Avatar answered Sep 20 '22 16:09

Arthur Ulfeldt