Generic words and methods
Factor handbook » The language » Objects


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:

A method combination which dispatches on a specified stack position:

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

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

Method definition:

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.