However, definterface can only declare signatures with type hints that
have no array arguments or return types. I've tried type hint syntax
like ^doubles and ^"[D".
To be more precise, both 1.2 and 1.3alpha4 can compile the code, but
give an error when trying to show the class details with
clojure.contrib.repl-utils/show.
I realize that arrays are java.lang.Object's, and so this probably
doesn't stop one from doing what you need to do (at least, I don't
think so). But it is at least incongruous with deftype and gen-class.
Below is some sample code, which I was testing using AOT compilation,
and then running the compiled code.
Andy
(ns reflect
(:use [clojure.contrib.repl-utils])
(:gen-class))
(definterface IDefinterfacePossibilities
;; These are all fine
(^void returnsVoid [])
(^int returnsInt [])
(^long returnsLong [])
(^Object returnsObjectWithExplicitReturnValueHint [])
(^String returnsString [])
(^java.util.HashMap returnsJavaUtilHashMap [])
(^void takesPrimitives [^boolean bool])
;; Uncommenting any one of the following lines give errors at run
;; time during the repl-utils/show call below.
;;(^ints returnsIntArrayTry1 [])
(^"[I" returnsIntArrayTry2 [])
;;(^void takesDoubleArrayTry1 [^doubles ds])
;;(^void takesDoubleArrayTry2 [^"[D" ds])
)
(defn -main [& args]
(doseq [c [IDefinterfacePossibilities]]
(println)
(printf "(class %s)=%s\n" c (class c))
(printf "(clojure.contrib.repl-utils/show %s)\n" c)
(clojure.contrib.repl-utils/show c))
(flush))
I believe I ran into this last year, and I think I may have even created
a patch that I intended to submit. I will take a look around my code
and see if I can find it. If so, I will post to the list and submit it
to JIRA.
Sincerely,
Daniel Solano Gómez
I have opened CLJ-737 for this issue and have included a patch that
should solve your problem. The following code now works:
(ns reflect
(:use clojure.contrib.repl-utils)
(:gen-class))
(definterface Test
(^void returnsVoid [])
(^int returnsInt [])
(^long returnsLong [])
(^Object returnsObjectWithExplicitReturnValueHint [])
(^String returnsString [])
(^java.util.HashMap returnsJavaUtilHashMap [])
(^void takesPrimitives [^boolean bool])
(^ints returnsIntArrayTry1 [])
(^"[I" returnsIntArrayTry2 [])
(^void takesDoubleArrayTry1 [^doubles ds])
(^void takesDoubleArrayTry2 [^"[D" ds])
(^void takesHashMapArray [^"[Ljava.util.HashMap;" ds])
(^"[[Ljava.net.InetAddress;" returnsTwoDInetAddressArray []))
(defn -main [& args]
(doseq [c [Test]]
(println (format "(class %s)=%s" c (class c)))
(println (format "(show %s)" c))
(show c))
(flush))