Clojure-idiomatic way to initialize a Java object?

As explained in the update to my question, here is the answer I came up with.

Up vote 6 down vote favorite share g+ share fb share tw.

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" rel="nofollow">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" rel="nofollow">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". Java clojure initialization idiomatic link|improve this question edited Nov 30 '10 at 14:40 asked Nov 30 '10 at 14:23Ralph5,13221036 99% accept rate.

As explained in the update to my question, here is the answer I came up with: (let url-connection (doto (java.net. HttpURLConnection. ) (.setDoInput true) ; more initialization )) ; use the url-connection ) Maybe someone can come up with a better one.

The code is as minimal as you can get. – kotarak Dec 1 '10 at 20:43 @kotarak: Perfection :-). – Ralph Dec 1 '10 at 21:12 :) Some say "Clojure".

;) – kotarak Dec 2 '10 at 14:10 @kotarak: I am currently reading "Land of Lisp" by Conrad Barski, MD (very good book! ). I am obviously no expert on Clojure, but as I read his examples, I already see how much easier (and efficient) it will be to do the implementations in Clojure, using Maps, Sets, and Vectors instead of just Lists, like in traditional Common Lisp.

– Ralph Dec 2 '10 at 14:21 I haven't read Barski's book, but CL does have arrays and hash tables. And I think that, at least at this point, Clojure might have a hard time matching the efficiency of the faster CL implementations. – T Duncan Smith Dec 2 '10 at 18:37.

Assuming that there is no constructor that accepts all the initialization parameters needed, then the way you did it is the only one I know. The one thing you could do is wrap it all in a function like this: (defn init-url-conn doInput ...other params.. (let url-conn (java.net. HttpURLConnection.

) (doto url-conn (.setDoInput true) ; more initialization on url-conn ) url-conn)) And call with: (let url-connection (let url-conn (init-url-con true ...other params..) ; use the url-connection ) However, this is specific per object and it is really useful only if you are initializing object of that class more than once. Also you could write a macro that accepts all method names, and params and does this. But, when called, that call wouldn't be much shorter than your first example.

If anyone has a better idea, I'd like to see it, since I was asking myself the same just the other day..

Goric: Check out my update. It may be the answer. – Ralph Nov 30 '10 at 14:41 1 Yes.

And doc for doto has a similar example: (doto (new java.util. HashMap) (.put "a" 1) (.put "b" 2)) – Goran Jovic Nov 30 '10 at 14:44 Thanks! Your update indeed is the answer – Goran Jovic Nov 30 '10 at 14:46.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions