Parametric Polymorphism (Generics)¶
Functions and data structures can be written to work with any type while preserving type safety - the algorithm is type-agnostic but the compiler still enforces type consistency.
Tension Addressed¶
Expressiveness vs. Simplicity; Safety vs. Flexibility
Recognizable As¶
Container types that hold "any type T"; functions like "sort" that work on any comparable type; type parameters in angle brackets or square brackets; the "generic" or "template" keyword
Canonical Sources¶
Strachey, "Fundamental Concepts in Programming Languages" (1967); Reynolds, "Types, Abstraction and Parametric Polymorphism" (1983)
Transfers To¶
Generic algorithms, template metaprogramming, any abstraction that factors out the "what" from the "how"abstractionembody a profound separation of concerns: the container/algorithm doesn't care about the specific type, only its capabilities. This is the type-theoretic equivalent of "program to an interface, not an implementation."
Sub-concepts: - Bounded polymorphism: Type parameters with constraints (e.g., "T must be comparable") - Variance: How subtyping relationships transfer through generic types (covariance, contravariance, invariance)
Related Concepts¶
- Abstraction - Software Architecture
- Separation of Concerns - Software Architecture