Reputation: 1887
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.
Upvotes: 4
Views: 659
Reputation: 26436
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.
Upvotes: 13