Parametric Polymorphism



next up previous contents index
Next: Programs Up: Overview of the Previous: Modules

Parametric Polymorphism

In addition to subtype polymorphism, Theta supports _parametric polymorphism_, in which a routine, type, or class definition is parameterized by one or more types (9.3). Parametric polymorphism allows the actual parameter type to be selected by the user, when the type or routine is _instantiated_ (3.2.2). For example, "array" is a parameterized type, with instantiations "array[int]", "array[char]", and so on.

Parameters can be constrained to be types whose objects have certain methods with certain signatures. These constraints permit parameterized code to be compiled without knowledge of the actual instantiation types. Generic code needs to be compiled only once.

Subtype polymorphism is useful for defining generic behavior over a set of related types, while parametric polymorphism is useful for defining generic behavior where the actual parameters need not be related in the type hierarchy.



theta-questions@lcs.mit.edu