Protocol slots
Factor handbook » The language » Objects » Tuples

Prev:Tuple slot declarations
Next:Tuple introspection


A protocol slot is one which is assumed to exist by the implementation of a class, without being defined on the class itself. The burden is on subclasses (or mixin instances) to provide this slot.

Protocol slots are defined using a parsing word:
SLOT:


Protocol slots are used where the implementation of a superclass needs to assume that each subclass defines certain slots, however the slots of each subclass are potentially declared with different class specializers, thus preventing the slots from being defined in the superclass.

For example, the growable mixin provides an implementation of the sequence protocol which wraps an underlying sequence, resizing it as necessary when elements are added beyond the length of the sequence. It assumes that the concrete mixin instances define two slots, length and underlying. These slots are defined as protocol slots: SLOT: length and SLOT: underlying. An alternate approach would be to define growable as a tuple class with these two slots, and have other classes subclass it as required. However, this rules out subclasses defining these slots with custom type declarations.

For example, compare the definitions of the sbuf class,
TUPLE: sbuf { underlying string } { length array-capacity } ; INSTANCE: sbuf growable

with that of the vector class:
TUPLE: vector { underlying array } { length array-capacity } ; INSTANCE: vector growable