Provide a constructor with Clojure reify
-
09-10-2019 - |
문제
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?
해결책
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.
제휴하지 않습니다 StackOverflow