(rontolisp) docs
← Protocols, records and types

deftype

(deftype Name [fields...] Protocol (method [target & args] body...) ...)

Defines a deftype: a record-shaped value opaque to the map verbs. The value shares the record's shape, class name included, with a :C%TYPE tag; reads miss (get answers the default), writers and seq/count/empty? signal, and = is identity, like the oracle, unless the body implements the interface the function reads (below). Only the positional constructor ->Name lowers (the oracle defines no map->Name for deftypes); (Name. ...) rewrites to it. Inline method bodies see the fields as locals, like defrecord. A field marked ^:unsynchronized-mutable or ^:volatile-mutable is private to those methods, which assign it with set!. An instance call reaches the inline methods and the immutable fields (.name). The name joins the whole-file pre-scan. The name in value position is the type's class, like a record's (user.T, what class answers for its values).

The body may implement the clojure.lang interfaces the core functions consult, the collection interfaces included, and override Object's methods, like reify's, its methods seeing the fields: a deftype of Counted counts, one of IDeref derefs, one of IObj carries the metadata its own methods keep, and one of IPersistentMap is a map to assoc, get, map? and the printer.