\a ;; compiles to "a"
:a ;; compiles to "a"
"a" ;; compiles to "a"
This is not meant as destructive criticism; I'm genuinely interested in the motivation for that. \a ;; compiles to "a"
:a ;; compiles to "a"
"a" ;; compiles to "a"
This is not meant as destructive criticism; I'm genuinely interested in the motivation for that. user=> (type \a)
java.lang.Character
user=> (type :a)
clojure.lang.Keyword
user=> (type "a")
java.lang.String
The only unusual thing is the keyword. Fogus and Houser say this: "Because keywords are self-evaluating and provide fast equality checks, they're almost always used in the context of map keys." But here you're back to checking equality of strings. Not sure if the same is true for ClojureScript.You can also use them as functions to look up values in maps. For example:
user=> (:my-key {:other-key 1, :my-key 2})
2
There are other places they're used--for example, list comprehensions with `for`: (for [x (range 10)
y (range 10)
:when (= x y)]
[x y])
=> ([0 0] [1 1] [2 2] [3 3] [4 4] [5 5] [6 6] [7 7] [8 8] [9 9])
In Clojure keywords are nice to have.The coolest thing about :keywords in Clojure is that they really are functions and you can do things like (map :keywords ontoSomething) to extract the same property out of many things. It don't work in Wisp.
That's basically what keywords do -- they are human-readable and compile down (prob. using some hash) into some integer, and the integer is stored globally only once. The biggest savings in memory comes when using maps, and that's how & why keywords get their name.
Also, Java declared java.lang.String as final, so Strings can't be extended to implement clojure interfaces to get nice behavior, like IFn and IKeywordLookup.
Ok. That sucks. So pretty much anyone already a bit serious about Clojure wont be enticed by this then.
Edit: From the page:
;; Keywords can be invoked as functions, that desugars to plain
;; associated value access in JS
(:bar foo) ;; => foo["bar"]
That seems pretty close to what's expected though, doesn't it? public class Keyword implements IFn, Comparable, Named, Serializable, IHashEq
https://github.com/clojure/clojure/blob/master/src/jvm/cloju... \a ;; becomes 'a' in java. The character of 'a'
:a ;; a keyword of :a. More efficient than using 'a'. Also it can act as a function. If you have a map with a key of :a in it, instead of having to say "get me the value for :a" you can say (:a map)
"a" becomes "a" in java. The string of "a"As of :a keyword it's a different story. As a matter of fact actual (quoted) keyword ':a does not compiles to "a" string, but general references do. That is because in JS constant string literals like "load", "DOMContentLoaded" are used in cases where idiomatic Clojure(script) would have used :load :DOMContentLoaded keywords, there for it made sense to just compile those keywords to semantical analogues in JS, same as `foo-bar` lisp naming convention compiles to adequate `fooBar` naming in JS.