java

2018-05-09T01:55:58.000029Z

That all makes sense. Thanks a ton @seancorfield. I hope i can pay it forward one day 🍻

2018-05-09T03:15:14.000110Z

I have a java class which I would like to use right out of the box. However I find myself writing clojure wrappers over the methods. Example:

(defn a-method [objInst arg1 arg2]
(.aMethod objInst arg1 arg2))
I find myself doing this for a lot of methods. Is there a more idiomatic way of doing this. Java method is aMethod, in clojure java interop I have to use it as (.aMethod objInst arg1 arg2) Would like to see this: (aMethod objInst arg1 arg2)

2018-05-09T03:22:06.000163Z

I would recommend getting over it, (.aMethod objInst arg1 arg2) is fine

2018-05-09T03:22:42.000122Z

creating wrappers that don't add any functionality is gross

seancorfield 2018-05-09T03:24:24.000093Z

@dhruv1 I'll agree with @hiredman -- wrappers that add nothing beyond a tiny bit of syntactic sugar is a waste of time. Clojure is a hosted language: it is designed to be used with Java interop.

seancorfield 2018-05-09T03:25:25.000111Z

If the (Java) classes are actually awkward to use (perhaps the dreaded "Builder" pattern), then a wrapper can improve life.

2018-05-09T03:25:30.000211Z

@hiredman and @seancorfield thank you for the quick reply! if that’s the most idomatic way, then i’ll go ahead with that

2018-05-09T03:32:20.000048Z

when i use those methods, it didn’t seem very “clojurefied” to me. another way I got to work around it was I defined a protocol which defines the aMethod, bMethod, … etc. these are the methods defined by the interface my java class implements. public class AClass implements AnInterface I defined a protocol which defines all the methods in AnInterface. I then extend the protocol to AClass and then in the methods, I calls the AClass’s super method.

2018-05-09T03:32:31.000120Z

better explained by this code:

2018-05-09T03:32:46.000213Z

public class AClass implements AnInterface

(defprotocol MyProtocol
  (aMethod [this arg1 arg2]))


(extend-protocol MyProtocol
  AClass
  (aMethod [this arg1 arg2]
    (.aMethod this arg1 arg2)))

2018-05-09T03:35:00.000201Z

i’m doing this so that the (.aMethod ...) call is contained within my namespace which is wrapping the java class. I am wrapping this over a kind of system logger. so (.aMethod ...) will be spread across my codebase. I just wanted to contain the java interop within a namespace

seancorfield 2018-05-09T03:41:11.000145Z

Seriously, don't.

seancorfield 2018-05-09T03:41:35.000185Z

You're doing all that work to eliminate a single . in the calls!

2018-05-09T03:42:32.000237Z

yeah… it was getting complicated while i was explaining it.

2018-05-09T03:42:47.000182Z

thank you, it makes sense to avoid this. 🙂

seancorfield 2018-05-09T03:43:05.000173Z

Java interop is part of Clojure. It's Clojurefied almost by definition 🙂

2018-05-09T03:43:52.000060Z

gotcha! awesome, thank you again for taking your time and helping out! 🙂

2018-05-09T03:44:11.000137Z

at this point I’m going to call it a night. Good Night!