ClojureDocs

Nav

Namespaces

reify

clojure.core

Available since 1.2 (source)
  • (reify & opts+specs)
reify creates an object implementing a protocol or interface.
reify is a macro with the following structure:
(reify options* specs*)

Currently there are no options.
 Each spec consists of the protocol or interface name followed by zero
or more method bodies:
 protocol-or-interface-or-Object
(methodName [args+] body)*
 Methods should be supplied for all methods of the desired
protocol(s) and interface(s). You can also define overrides for
methods of Object. Note that the first parameter must be supplied to
correspond to the target object ('this' in Java parlance). Thus
methods for interfaces will take one more argument than do the
interface declarations.  Note also that recur calls to the method
head should *not* pass the target object, it will be supplied
automatically and can not be substituted.
 The return type can be indicated by a type hint on the method name,
and arg types can be indicated by a type hint on arg names. If you
leave out all hints, reify will try to match on same name/arity
method in the protocol(s)/interface(s) - this is preferred. If you
supply any hints at all, no inference is done, so all hints (or
default of Object) must be correct, for both arguments and return
type. If a method is overloaded in a protocol/interface, multiple
independent method definitions must be supplied.  If overloaded with
same arity in an interface you must specify complete hints to
disambiguate - a missing hint implies Object.
 recur works to method heads The method bodies of reify are lexical
closures, and can refer to the surrounding local scope:

(str (let [f "foo"] 
     (reify Object 
       (toString [this] f))))
== "foo"
 (seq (let [f "foo"] 
     (reify clojure.lang.Seqable 
       (seq [this] (seq f)))))
== (\f \o \o))

reify always implements clojure.lang.IObj and transfers meta
data of the form to the created object.

(meta ^{:k :v} (reify Object (toString [this] "foo")))
== {:k :v}