Generic words and methods
Factor handbook > The language > Objects

Prev:Tuples
Next:Delegation


A generic word is composed of zero or more methods together with a method combination. A method specializes on a class; when a generic word is executed, the method combination chooses the most appropriate method and calls its definition.

A generic word behaves roughly like a long series of class predicate conditionals in a cond form, however methods can be defined in independent source files, reducing coupling and increasing extensibility. The method combination determines which object the generic word will dispatch on; this could be the top of the stack, or some other value.

Generic words which dispatch on the object at the top of the stack:
GENERIC:


A method combination which dispatches on a specified stack position:
GENERIC#:


A method combination which dispatches on the value of a variable at the time the generic word is called:
HOOK:


A method combination which dispatches on a pair of stack values, which must be numbers, and upgrades both to the same type of number:
MATH:


Method definition:
M:


Generic words must declare their stack effect in order to compile. See Stack effect declarations.
Method precedence
Calling less-specific methods
Custom method combination
Generic word introspection

Generic words specialize behavior based on the class of an object; sometimes behavior needs to be specialized on the object's structure; this is known as pattern matching and is implemented in the match vocabulary.