Object-oriented class system: classes-as-objects, message passing, single inheritance.
Instances: (obj name args…) – method wins, else member (obj m)/(obj m v).
Classes are callable: (Class name args…) – static method, (Class new …) to
instantiate, else class-wide member (Class m)/(Class m v). Use classes as
namespaces of static methods. Raw member access in methods: (member ‘m)/(set-member! ‘m v).
%class-call-handler / %bind-call-over! are the PUBLIC value-call extension hooks
(the % marks handler-layer machinery, not module privacy): (%bind-call-over! (Type of v) Class)
routes a value’s symbol-selector calls to the class’s statics, subject-LAST.
method-ofResolve a static method to a bare closure for hot-loop direct calls.
The sanctioned de-dispatch door: resolve once, call directly in the hot
loop with the class as argument 0 – ((method-of C ‘step) C cur v).
Do NOT (wrap …) the handle; a stored method already evaluates its
args exactly once at direct call.
Parameters:
CLASS — Class to resolve againstSYMBOL — Static-method selectorReturns: CALLABLE — The resolved static method closure, or nil
See also: method-ref
superInvoke the parent class’s version of a method.
Selector is literal: (super self method args…). Instance methods only.
Resolves from the parent of the method’s DEFINING class, so it is correct
through multi-level inheritance.
See also: def-class
method-refMake a class/instance method usable as a first-class function value.
Selector is literal: (method-ref Class method). Works for static and instance methods.
Examples:
(List map (method-ref Str upcase) (list "a" "b")) => ("A" "B")See also: def-class
object?Test whether a value is an object instance.
Parameters:
ANY — Value to testReturns: BOOL — True if x is an object instance
See also: class?
class?Test whether a value is a class.
Parameters:
ANY — Value to testReturns: BOOL — True if x is a class
See also: object?
class-ofReturn the class an instance belongs to (itself a callable class object).
Parameters:
OBJECT — InstanceReturns: CLASS — The class an instance belongs to
See also: class-name
class-nameReturn the name symbol of a class, or of an instance’s class.
Parameters:
ANY — An instance or a classReturns: SYMBOL — The class name
See also: class-of
class-parentReturn a class’s parent class (the one it extends), or nil if it has none.
Parameters:
CLASS — A classReturns: CLASS — The parent class, or nil for a root class
See also: class-name
instance-of?Test whether an instance belongs to a class or any of its descendants.
Parameters:
OBJECT — InstanceCLASS — ClassReturns: BOOL — True if inst is a class or one of its subclasses
See also: object?
class-membersList a class’s own instance member names (not inherited).
Parameters:
CLASS — A classReturns: LIST — This class’s own instance-member names
See also: class-methods
class-methodsList a class’s own instance method names (not inherited).
Parameters:
CLASS — A classReturns: LIST — This class’s own instance-method names
See also: class-members
class-static-membersList a class’s own static (class-wide) member names (not inherited).
Parameters:
CLASS — A classReturns: LIST — This class’s own static-member names
See also: class-static-methods
class-static-methodsList a class’s own static method names (not inherited).
Parameters:
CLASS — A classReturns: LIST — This class’s own static-method names
See also: class-static-members
def-classDefine a class (a callable class object) with fields, methods, and statics.
Names are literal (no quotes). Body forms (members and methods intermixed):
NAME (NAME default) instance member (default optional, nil if omitted;
evaluated per construction, so (links (Set make)) is fresh each time)
(doc DECL “desc” meta..) document a member; DECL is NAME or (NAME default)
(method NAME (self . args) body…) instance method
(static MEMBER… (method …)…) class-wide members + static methods
(interface NAME…) abstract: a concrete subclass must implement each NAME
(private DECL…) (protected DECL…) visibility blocks (members and methods; also inside (static …)):
private = defining class's methods only; protected = its chain.
Enforced at the dispatch door; introspection and (help) still list them.
(doc “summary” (note ..) (see ..) (example ..)) class-level docs, shown by (help Class)
A method shadows a member of the same name. Parent: () or (extends Class).
Inside a method, (self m) accesses members; (member ‘m)/(set-member! ‘m v) are raw.
A (method %init (self) …) runs after every construction, fields built –
the initialize hook; a child’s override wins, (super self %init) chains.
Examples:
(do (def-class C () (static (n 7) (method get (self) (self n)))) (C get)) => 7See also: new
newConstruct an instance inline; names literal, values evaluated.
Inline construction: member names are literal (bare, not quoted) and
values are expressions, evaluated in the caller’s env:
(new C name val name val …) plist form – the usual one
(new C (name . val) …) dotted-alist form (val is an expression)
(new C v1 v2 … name val …) positional prefix: values fill members in
constructor order (root ancestor's members first, then each subclass's own),
until the first bare declared-member name starts the keyword tail.
A TRAILING bare member name is positional ((new Distances root) passes the root
variable); the footgun: a NON-trailing positional value spelled as a bare member
name (or a call headed by one) reads as the keyword tail – use keywords there.
For a computed/quoted store (a list of ready values) use new-from.
Examples:
(do (def-class P () x) ((new P x 5) x)) => 5See also: new-from
new-fromInstantiate a class from a computed option store (alist or plist) of values.
Data counterpart to new: the store is evaluated (new-from is a fn) and its
values are used as-is, not re-evaluated – so pass a quoted list, a variable,
or a built alist/plist. Unknown keys fall back to declared defaults.
Parameters:
CLASS — The class to instantiateLIST — Ready-value store: alist ((k . v) …) or plist (k v …)Examples:
(do (def-class P () x y) ((new-from P '(x 1 y 2)) x)) => 1See also: new