This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-04
Channels
- # architecture (27)
- # bangalore-clj (4)
- # beginners (22)
- # boot (35)
- # cider (26)
- # cljs-dev (2)
- # cljsrn (3)
- # clojure (156)
- # clojure-austria (3)
- # clojure-dev (9)
- # clojure-italy (25)
- # clojure-nl (10)
- # clojure-poland (5)
- # clojure-sanfrancisco (1)
- # clojure-spec (6)
- # clojure-uk (64)
- # clojurescript (169)
- # core-async (13)
- # cursive (13)
- # datomic (63)
- # dirac (50)
- # duct (21)
- # editors (1)
- # emacs (6)
- # events (1)
- # fulcro (1)
- # java (22)
- # keechma (14)
- # leiningen (2)
- # luminus (4)
- # off-topic (23)
- # onyx (4)
- # parinfer (5)
- # pedestal (4)
- # re-frame (6)
- # reagent (4)
- # ring-swagger (7)
- # rum (4)
- # shadow-cljs (84)
- # specter (5)
- # sql (36)
- # tools-deps (76)
- # uncomplicate (3)
- # yada (4)
i have a question regarding implementing java public interfaces in clojure
here is an example of my interface
`
public interface SomePublicInterface {
void aMethod(Enum arg1, String... arg2),
void aMethod (Enum arg1, AClass arg2, String... arg3)
}
Here is how i’m implementing it:
(defrecord SomeRecord [field1 name]
SomePublicInterface
(aMethod [this enum msg] (str "ENUM is " enum)))
(defn some-record
[name]
(->SomeRecord (.getTheThing (TheThingFactory/getInstance) "NamedThing") name))
(def test (some-record "testing-some-record-method"))
(aMethod test "ENUM" "MY TEST") ;;doesn't work
Unable to resolve symbol: aMethod in this context
(.aMethod test "ENUM" "MY TEST")
ClassCastException Cannot cast java.lang.String to java.lang.Enum java.lang.Class.cast (Class.java:3369)
it defaults to the types of the actual java Interace.
it doesn’t call the aMethod
i implemented.
I would like to call aMethod
on my record (type) with strings and i want it to print “ENUM is: MY TEST”
if I had to guess, from the exception, the interface you implemented requires it to return an Enum and you are returning a string (which doesn't match the example interface, but given the exception that is my guess)
to be honest i don’t know much about java.
i have to use a class that’s implementing the Interface SomePublicInterface
I just thought it would return back a string because that’s what i’m asking for.
the return type on aMethod
in the interface is void
I’m importing the Interface like this:
(import '[this.is.my.package SomePublicInterface])
@dhruv1 aMethod
is expecting an Enum
and an array of String
(the varargs part) or an Enum
, an AClass
(whatever that is), and an array of String
. Your implementation must match the signatures -- otherwise it isn't implementing the interface.
A string is not an Enum, but if you have a specific enumeration class, you can construct an Enum type from that class and the name used for one of the enumeration class members via (Enum/valueOf TheClass "name")
.
hey there. I am using a proxy to create an ENUM
(defn ->enum
[^String enum]
(proxy [Enum] [enum 1]))
In the code above you were trying to pass a string to the Enum argument and, quite correctly, got the exception "ClassCastException Cannot cast java.lang.String to java.lang.Enum"
right, so i was able to get past that by using a proxy and (into-array java.lang.String ["MY TEST"]
i am still at a loss here because of my lack of knowledge in Java and somewhat clojure
Were you then able to call (.aMethod test (->enum "ENUM") (into-array String ["MY TEST"]))
?
That Java interface looks like you made up the names rather than show us what the real code is -- are you working with a publicly available Java library? Or are you writing Java code yourself and compiling it and bringing it into your Clojure code?