Fork me on GitHub
#java
<
2018-05-09
>
Drew Verlee01:05:58

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

dhruv103:05:14

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)

hiredman03:05:06

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

hiredman03:05:42

creating wrappers that don't add any functionality is gross

seancorfield03:05:24

@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.

seancorfield03:05:25

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

dhruv103:05:30

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

dhruv103:05:20

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.

dhruv103:05:31

better explained by this code:

dhruv103:05:46

public class AClass implements AnInterface

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


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

dhruv103:05:00

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

seancorfield03:05:11

Seriously, don't.

seancorfield03:05:35

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

dhruv103:05:32

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

dhruv103:05:47

thank you, it makes sense to avoid this. 🙂

seancorfield03:05:05

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

dhruv103:05:52

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

dhruv103:05:11

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