Edward
Edward

Reputation: 4651

How to call several (static) Java-methods in a row in Clojure

So far I found two ways to call static methods from Java as functions in Clojure.

;; using .
(. Math min 12 13)
;; using /
(Math/min 12 13)

The calls can become more complicated when using return-values of one call, for the next call.

(. (. System out) println "Static call from Clojure.")

It seems to not work using the "/" notation:

((System/out) /println "Static call from Clojure.")

Probably the "."-notation is meant to handle this kind of method-calls. In my opinion the "."-notation becomes difficult to read even when there are only two method calls in a row.

Is there a clearer way to call methods in a row?

Upvotes: 1

Views: 165

Answers (2)

Chiron
Chiron

Reputation: 20245

Are you aware of this notation?

 (.. System (getProperties) (get "os.name"))

Notice the two dots.

Which will expand to:

(. (. System (getProperties)) (get "os.name"))

Java Interop on Clojure.org.

Upvotes: 4

Abimaran Kugathasan
Abimaran Kugathasan

Reputation: 32488

Try following.

(-> (System/out) (.println "Static call from Clojure."))

Upvotes: 1

Related Questions