I'm learning Clojure. Still I have no good understanding for the language and the philosophy.
But I want to be more familiar with the language. Hence
I have started to read Clojure core API documentation
and found some interesting stuffs in clojure.core/get source code.
(defn get
"Returns the value mapped to key, not-found or nil if key not present."
{:inline (fn [m k & nf] `(. clojure.lang.RT (get ~m ~k ~@nf)))
:inline-arities #{2 3}
:added "1.0"}
([map key]
(. clojure.lang.RT (get map key)))
([map key not-found]
(. clojure.lang.RT (get map key not-found))))
To get a value with given key the code uses clojurelang.RT/get function.
The code calls dot operator - (. clojure.lang.RT (get map key)).
My question is why the author wrote (. clojure.lang.RT (get map key)) instead of
(clojure.lang.RT/get map key).
Is there any technical difference? or any benefit?
The dot in Clojure is used for host interop (with the Java class clojure.lang.RT in this case). The idiomatic form for a static method is (Classname/staticMethod args*) but that gets expanded into a call on the . special form. In the case of the get function, you're essentially looking at a part of Clojure's implementation. There's no reason why the lower-level Clojure code would use the higher level macro - so it uses the . form directly.
Take a look at the documentation at: http://clojure.org/java_interop
The idiomatic forms are at the top and below you can find how they're expanded into calls on the dot operator. Here's the relevant bit for static methods:
(Classname/staticMethod args*) ==> (. Classname staticMethod args*)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With