I was reading Programming Clojure and came across the following code at src/examples/gulp.clj. I can't follow what does the dot after StringBuilder, FileInputStream etc mean -
(ns examples.gulp
(:import (java.io FileInputStream InputStreamReader BufferedReader)))
(defn gulp [src]
(let [sb (StringBuilder.)]
(with-open [reader (-> src
FileInputStream.
InputStreamReader.
BufferedReader.)]
(loop [c (.read reader)]
(if (neg? c)
(str sb)
(do
(.append sb (char c))
(recur (.read reader))))))))
Can anyone please shed some light.
The dot after a class name is just special syntax for calling a constructor of the class.
(macroexpand '(StringBuilder. "foo"))
;=> (new StringBuilder "foo")
Take a read through http://clojure.org/java_interop.
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