Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of dot after a symbol in clojure

Tags:

clojure

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.

like image 654
Raminder Avatar asked Dec 12 '22 17:12

Raminder


1 Answers

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.

like image 114
A. Webb Avatar answered Feb 01 '23 03:02

A. Webb