I am trying to find a Clojure-idiomatic way to initialize a Java object. I have the following code:
(let [url-connection
(let [url-conn (java.net.HttpURLConnection.)]
(doto url-conn
(.setDoInput true)
; more initialization on url-conn
)
url-conn)]
; use the url-connection
)
but it seems very awkward.
What is a better way to create the HttpURLConnection object and initialize it before using it later in the code?
UPDATE: It seems that (doto ...) may come in handy here:
(let [url-connection
(doto (java.net.HttpURLConnection.)
(.setDoInput true)
; more initialization
))]
; use the url-connection
)
According to the doto docs, it returns the value to which it is "doing".