gen-interface is a Clojure macro for generating a Java interface. It separates interface declaration from concrete inheritance, supporting “a-la carte polymorphism”: a type can adopt the generated contract without inheriting an implementation. The macro accepts :name, :extends, and :methods options and returns the generated type. section 15.0 section 15.3
Use the key-value options to define the interface contract:
- :name supplies the interface’s qualified name, such as "user.BookInterface". section 15.3
- :extends lists parent interfaces, allowing the generated interface to inherit existing interface contracts. section 15.3
- :methods specifies the method signatures exposed by the interface. section 15.3
Generate and implement a marker interface
(gen-interface :name "user.BookInterface" :extends [java.io.Serializable]) (ancestors user.BookInterface) ;; #{java.io.Serializable} (reify user.BookInterface Object (toString [_] "A marker interface for books.")) ;; #object[user$eval20 ... "A marker interface for books."] The example creates user.BookInterface, makes it extend java.io.Serializable, verifies that relationship with ancestors, and implements it with reify. gen-interface declares the contract; reify supplies an implementation for an object. section 15.0
