Is it by design that definterface cannot declare sigs with arrays as args or return vals?

57 views
Skip to first unread message

Andy Fingerhut

unread,
Feb 9, 2011, 7:55:21 PM2/9/11
to cloju...@googlegroups.com
In some testing I've done on 1.2 and 1.3 alpha4, deftype can implement
methods, using type hints in the Clojure method definitions, for a
Java-declared interface with array arguments and/or return types. So
can gen-class :methods

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))

Daniel Solano Gomez

unread,
Feb 12, 2011, 9:59:51 PM2/12/11
to cloju...@googlegroups.com
On Wed Feb 9 16:55 2011, Andy Fingerhut wrote:
> In some testing I've done on 1.2 and 1.3 alpha4, deftype can implement
> methods, using type hints in the Clojure method definitions, for a
> Java-declared interface with array arguments and/or return types. So
> can gen-class :methods
>
> 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

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

Daniel Solano Gomez

unread,
Feb 13, 2011, 2:30:13 PM2/13/11
to cloju...@googlegroups.com
> On Wed Feb 9 16:55 2011, Andy Fingerhut wrote:
> > In some testing I've done on 1.2 and 1.3 alpha4, deftype can implement
> > methods, using type hints in the Clojure method definitions, for a
> > Java-declared interface with array arguments and/or return types. So
> > can gen-class :methods
> >
> > 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

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))

Reply all
Reply to author
Forward
0 new messages