liveBook · Manning

Reify

As explained in Clojure: The Essential Reference

reify is a Clojure special form that creates an object implementing Java interfaces or extending a Java class with inline method implementations.

`reify` creates a one-off object that implements one or more interfaces, with method bodies written inline. Use it when a Java API needs a listener or another short-lived interface implementation, and maintaining a named class would add little value. It can implement interfaces, but it cannot extend a Java class. section 15.6 section 15.0

Register a property-change listener

Suppose `ClassWithProperty` fires an event when its value changes. The reified object supplies the required `PropertyChangeListener` method: ```clojure (import 'java.beans.PropertyChangeListener) (import 'ClassWithProperty) (let [observed (ClassWithProperty.) listener (reify PropertyChangeListener (propertyChange [this evt] (let [{:keys [oldValue newValue]} (bean evt)] (println "Button Clicked!" oldValue newValue))))] (.addPropertyChangeListener observed listener) (.setValue observed "I click")) ;; Button Clicked! nil I click ``` The listener is created where it is registered; no separate listener class is needed. section 15.6

Supply a protocol implementation

A protocol has an interface that `reify` can implement. Here, a wrapper supplies `IKVReduce` so `reduce-kv` can process entries from a Java `HashMap`: ```clojure (import 'java.util.HashMap) (import 'clojure.core.protocols.IKVReduce) (def m (doto (HashMap.) (.put :a "a") (.put :b "b") (.put :c "c"))) (reduce-kv (fn [acc k v] (assoc acc (str k) v)) {} (reify IKVReduce (kv-reduce [this f init] (reduce-kv f init (into {} m))))) ;; {":b" "b", ":c" "c", ":a" "a"} ``` Calling `reduce-kv` on `m` directly instead raises a missing-implementation exception. section 15.6

Choose `proxy` when a Java framework requires class extension or another proxy feature. For quick interface-only instances, `reify` is generally faster, though proxy class caching can help with interfaces that have many methods. Reducers and folders also use `reify` to give collections reduction or folding behavior. section 15.5 section 15.6 section 7.1.2

How Clojure: The Essential Reference covers it

How deep it goes
Works through inline listener and reduction-method implementations, then compares proxy behavior without detailing generated-class internals.
Examples
Shows a property-change listener and an IKVReduce wrapper around a Java HashMap.
What it assumes
Assumes familiarity with Clojure method forms and Java interfaces; illustrates the required methods through examples.
What you can do after
Apply it via `reify` to register an inline listener or supply a protocol implementation.
How it connects
Connects Java interfaces and Clojure protocols with proxy, reducers, and folders.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.