Search code examples
constructorclojurereify

Provide a constructor with Clojure reify


When using reify in Clojure, how can I provide an expression for the constructor?

Alternatively, how can I pass arguments to the base class constructor?


Solution

  • You cannot use reify to subclass & instantiate classes -- it's only meant for use with protocols and interfaces. (You can provide implementations of methods of Object, though.)

    To instantiate anonymous subclasses of arbitrary classes and/or interfaces, use proxy. All arguments to the superclass ctor of a proxy, if any, go into the args vector (the second argument) of the proxy form:

    (proxy [SomeClass SomeInterface-1 ...] [ctor-arg-1 ...]
      ; method impls follow
      ...
      )
    

    See (doc proxy) for more details.