That all makes sense. Thanks a ton @seancorfield. I hope i can pay it forward one day 🍻
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)I would recommend getting over it, (.aMethod objInst arg1 arg2) is fine
creating wrappers that don't add any functionality is gross
@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.
If the (Java) classes are actually awkward to use (perhaps the dreaded "Builder" pattern), then a wrapper can improve life.
@hiredman and @seancorfield thank you for the quick reply! if that’s the most idomatic way, then i’ll go ahead with that
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.
better explained by this code:
public class AClass implements AnInterface
(defprotocol MyProtocol
(aMethod [this arg1 arg2]))
(extend-protocol MyProtocol
AClass
(aMethod [this arg1 arg2]
(.aMethod this arg1 arg2)))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
Seriously, don't.
You're doing all that work to eliminate a single . in the calls!
yeah… it was getting complicated while i was explaining it.
thank you, it makes sense to avoid this. 🙂
Java interop is part of Clojure. It's Clojurefied almost by definition 🙂
gotcha! awesome, thank you again for taking your time and helping out! 🙂
at this point I’m going to call it a night. Good Night!