Delegation
Factor handbook » The language » Objects

Prev:Generic words and methods
Next:Mirrors


The delegate vocabulary implements run-time consultation for method dispatch.

A protocol is a collection of related generic words. An object is said to consult another object if it implements a protocol by forwarding all methods onto the other object.

Using this vocabulary, protocols can be defined and consultation can be set up without any repetitive boilerplate.

Unlike Tuple subclassing, which expresses is-a relationships by statically including the methods and slots of the superclass in all subclasses, consultation forwards generic word calls to another distinct object.

Defining new protocols:
PROTOCOL:

define-protocol ( protocol wordlist -- )


Defining new protocols consisting of slot accessors:
SLOT-PROTOCOL:


Defining consultation:
BROADCAST:

CONSULT:

define-consult ( consultation -- )


Every tuple class has an associated protocol consisting of all of its slot accessor methods. The delegate.protocols vocabulary defines formal protocols for the various informal protocols used in the Factor core, such as Sequence protocol, Associative mapping protocol or Stream protocol. Other vocabularies, like ui.theme also declare protocols.