3

It seems that Clojure 1.2.0 has a definterface form, apparently for creating Java interfaces, and some people recommend using it (e.g. one answer to this number crunching question). However, I cannot seem to find any documentation or substantial examples of how to use it. Am I not looking in the right place, or is it actually such an early feature that it should not be used? I'm interested in pointers to documentation or examples demonstrating the features of definterface.

Community
  • 1
  • 1
Jouni K. Seppänen
  • 43,139
  • 5
  • 71
  • 100

1 Answers1

6

Have a look at the documentation of gen-interface.

The rough form is:

(definterface Foo
  [bar [Arg1Type Arg2Type] ReturnType]
  [sideEffects [int] void]
  ...)

EDIT: You are right. The interface is closer to that of defprotocol than that of gen-interface.

(definterface Foo
  [^int foo [x ^String y]]
  [^void bar [^ints is]])
kotarak
  • 17,099
  • 2
  • 49
  • 39
  • You shuld look at Protocols to. The are maybe better for what your looking for. http://clojure.org/protocols – nickik Oct 12 '10 at 13:19
  • Actually it seems that definterface does significant processing on the arguments before expanding to a gen-interface form. For one thing, it gets the datatypes from metadata tags. – Jouni K. Seppänen Oct 13 '10 at 10:53
  • This is still not quite complete, but I'll mark it as the correct answer. One thing that confused me a lot was that this fails: (ns test-interface (:import java.io.File)) (definterface Foo [^void foo [^File f]]) When I try to run a test based on this code, I get errors about "java.lang.ClassNotFoundException: java.lang.File". If I put the full path java.io.File in the type hint, it works. – Jouni K. Seppänen Oct 17 '10 at 18:47
  • You have to qualify all classnames. This is quite unfortunate, but `gen-interface` (as well as `gen-class` btw) expects `java.lang` as package on unqualified names. – kotarak Oct 18 '10 at 05:44