reify
(reify Protocol (method [target & args] body...) ...)
Answers a fresh value per evaluation: a single-shot map plus methods (never
proxy, which stays the java: surface). The rows of its methods belong to the
form, stored once in each protocol's table like the oracle's one class per reify
form; each instance carries the methods its evaluation made, closing over that
evaluation's locals. Two instances are never = to each other, like the oracle;
= is identity otherwise. Method
groups stand under protocol names, like extend-type. A method named again over
another parameter vector implements another of its arities.
A reify of clojure.core.protocols/CollReduce is a collection reduce, into and
transduce reduce through its coll-reduce
(clojure.core.reducers builds its reducers that way):
Host interfaces
A body may also implement the clojure.lang interfaces the core functions consult, spelled
with their package or imported (clojure.lang is no default import, like the oracle's), and
override Object's toString, equals and hashCode. A method is matched by its name and
parameter count against every group of the body, so a toString may stand under a protocol.
| Interface | Read by |
|---|---|
IReduceInit, IReduce | reduce (without an init through IReduce), into, transduce, run!, mapv, filterv, vec, set, group-by, frequencies |
IKVReduce | reduce-kv, update-vals, update-keys |
Seqable | seq and every function over a seq (first, map, filter, doseq, for ...), seqable? |
Counted | count, empty?, counted? |
Indexed (a Counted) | nth, vector destructuring, indexed? |
ILookup | get, get-in, a keyword's or symbol's call, map destructuring |
IFn (with Callable and Runnable) | a call, a function argument (map, filter ...), apply (through applyTo), ifn? |
IDeref | deref, @ |
IBlockingDeref | deref with a timeout and a timeout value |
IAtom, IAtom2 | swap!, reset!, compare-and-set!, swap-vals!, reset-vals! of a value that is no atom |
IRef, IReference | add-watch, remove-watch, set-validator!, get-validator; alter-meta!, reset-meta! |
IFn$OOL and the other primitive IFn interfaces | .invokePrim (a call of the value goes through IFn's invoke), instance? |
IMeta, IObj | meta, with-meta, vary-meta (deftype only: a reify carries metadata itself) |
java.lang.CharSequence | count (length), nth, seq and every function over a seq (charAt), seqable?; re-matcher and re-find read its characters (charAt) as far as a match needs, re-seq, re-matches, clojure.string and a string's .contains and .replace the string of all of them |
Object | str and printing (toString), = (equals), .hashCode and hash without IHashEq (hashCode) |
A definterface of the program defines an interface with no host class: the name is
bound to the binary name (my-app.core.Name, like a deftype's class), and a body
naming it stores its methods in the type's row under that name, in the same shape as
the rows above. instance? tests the name and a .method call dispatches on the
row's method, like the oracle's class. A signature is the method name over one
parameter vector ((m [x y]); without one, (m), a zero-argument method); a repeated
signature is refused naming the duplicate with its signature, and any other shape is
refused. Extending a protocol to the interface is refused as one that is not a
protocol.
instance? of the interface and an instance call of its method ((.count x)) reach the type
too, and so does a protocol extended to the interface. A method the body leaves out is the
oracle's AbstractMethodError when called (a java.util default method keeps the
interface's, refused by name when called). IType, IRecord and IKeywordLookup are
markers instance? reads (a type implementing IRecord is record?); any other
clojure.lang interface (IChunkedSeq ...) is refused by name. Any other public host interface (java.io.Closeable,
java.util.concurrent.ThreadFactory, java.util.concurrent.locks.Lock ...) stores its
methods in the type's row under its binary name, like a definterface's: instance? tests it
(its super-interfaces too), a .method call no core function answers calls the row's method,
and with-open closes a Closeable; a default method the body leaves out is refused when
called from Clojure, where the oracle runs the interface's. The Java interfaces a body implements (Runnable,
Comparable, java.util.Comparator, Iterable, a java.util collection, any host interface
above) and the Object methods it overrides are
what Java sees of the value too: it crosses into a Java member as an object implementing them
(Java interop). Deviations: a value overriding toString prints as
#object[user$reify "text"], without the oracle's class number and identity hash; a
re-matcher or re-find over a CharSequence reads it in prefixes of 16, 32, 64 ...
characters, so it may call charAt past where the oracle's matcher stops, and the other regex
functions and clojure.string read it whole through length and charAt once per call,
where the oracle's matcher reads only as far as it matches and most clojure.string
functions call toString.
Collection interfaces
A body implements a collection the core functions read through its methods, in the order the
oracle asks them, the interface's supers included (IPersistentMap is an Associative, an
Iterable and a Counted):
| Interface | Read by |
|---|---|
IPersistentCollection | conj, into, merge (cons), empty, = on either side (equiv), count of one that is no Counted (its seq walked), coll? |
Associative | assoc, update, assoc-in (assoc), contains? (containsKey), find, select-keys (entryAt), associative? |
IPersistentMap, MapEquivalence | dissoc (without), keys, vals, reduce-kv, map?, printing as a map; a map's = reads it as a java.util.Map only with MapEquivalence |
IPersistentSet | disj (disjoin), contains?, get and a keyword's call (contains, get), set?, printing as a set |
IPersistentStack | peek, pop |
IPersistentVector | vector?, printing as a vector, a vector's = (count, nth), subvec |
ISeq | a seq answering one walks its first and next; seq?, printing as a seq |
Sequential, IPersistentList | sequential?, list?; a sequential's = and nth walk its seq |
Reversible | rseq, reversible? |
IPending | realized? |
Sorted | subseq, rsubseq (seqFrom, seq, comparator, entryKey), sorted? |
java.lang.Comparable | compare, sort, a sorted collection's default order |
java.util.Comparator | sort, sort-by, sorted-map-by, sorted-set-by (compare) |
java.lang.Iterable | the seq of one that is no Seqable, reduce, into, vec (iterator), seqable? |
java.util.Iterator | iterator-seq, the seq and reduction of an Iterable |
java.util.Collection, List, Set, RandomAccess | count (size), nth (a RandomAccess list's get), contains? (a Set's contains), =, pr as a list, a vector or a set |
java.util.Map | get, contains?, find, count, seq (entrySet), =, pr as a map |
IHashEq | hash (hasheq), and the collection hash verbs over a member |
java.io.Serializable, IEditableCollection, the transients | instance? and instance calls; transient calls asTransient, persistent! and the bang verbs the transient interfaces' methods |
(.iterator coll) of a core collection, clojure.lang.SeqIterator and clojure.lang.RT/iter
answer an iterator over its seq, and clojure.lang.MapEntry builds a [k v] vector, the
map entry here. Deviations: first, next and rest of an ISeq type read it through its
seq (the oracle calls its first, next and more), so next and rest answer that
seq's tail; a verb may call a method another number of times than the oracle (no chunked
seqs); str of such a value spells its contents where the oracle answers Class@hash, and a
java.util one prints its contents under print too.
Map keys and set members
A value whose type has a hash of its own (IHashEq's hasheq, Object's hashCode) and an
equality of its own (IPersistentCollection's equiv, Object's equals, or a map, set or
sequential interface the core collections read) keys a map or a set by value, as the oracle's
hash map holds it. A map, set or sequential type finds, and is found by, the core collection =
to it (a data.priority-map as a set member); any other type finds a value = to it through
its own equality. The key stored first stays, as for a core key. A type without a hash of its
own keys by identity, like the oracle's hash map, which hashes it by its identity.
Deviations: every map holds its keys as the oracle's hash map does, while the oracle's array map
(up to eight entries) compares keys without hashing, so it finds an = value of a type with no
hash of its own, and compares a lookup key that is no java.util.Collection or Map by
equals alone. A map, set or sequential type is bucketed by its contents rather than its
hasheq, so one whose hasheq disagrees with its = is still found; any other type is
bucketed by its own hash, so a core collection its equiv calls equal is not found.