This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-09
Channels
- # aws (3)
- # beginners (61)
- # boot (1)
- # cider (80)
- # clara (5)
- # cljs-dev (5)
- # cljsrn (19)
- # clojure (40)
- # clojure-dev (29)
- # clojure-dusseldorf (2)
- # clojure-greece (21)
- # clojure-italy (16)
- # clojure-russia (7)
- # clojure-spec (31)
- # clojure-uk (70)
- # clojurescript (31)
- # cursive (21)
- # data-science (5)
- # emacs (7)
- # fulcro (23)
- # graphql (4)
- # java (18)
- # jobs (1)
- # jobs-discuss (25)
- # juxt (14)
- # nrepl (2)
- # off-topic (18)
- # om-next (2)
- # onyx (3)
- # re-frame (16)
- # reagent (50)
- # ring-swagger (5)
- # shadow-cljs (70)
- # specter (5)
- # sql (24)
- # tools-deps (16)
- # vim (41)
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)
@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.
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!
Java interop is part of Clojure. It's Clojurefied almost by definition 🙂