Method precedence
Factor handbook » The language » Objects » Generic words and methods

Prev:M:
Next:Calling less-specific methods


Conceptually, method dispatch is implemented by testing the object against the predicate word for every class, in linear order (Class linearization).

Here is an example:
GENERIC: explain ( object -- ) M: object explain drop "an object" print ; M: generic explain drop "a generic word" print ; M: class explain drop "a class word" print ;

The linear order is the following, from least-specific to most-specific:
{ object generic class }

Neither class nor generic are subclasses of each other, and their intersection is non-empty. Calling explain with a word on the stack that is both a class and a generic word will print a class word because class is more specific than generic in the class linearization order. (One example of a word which is both a class and a generic word is the class of classes, class, which is also a word to get the class of an object.)

The dispatch-order word can be useful to clarify method dispatch order:
dispatch-order ( generic -- seq )