Polymorphism (aka generic programming) is an approach that allows you to use the same function name with different data types.
Function is polymorphic if it works for several different types.
Function is monomorphic if it works only for one type.
The term polymorphism means the ability of the same identifier to represent multiple different types.
Kinds of polymorphism:
Kind of polymorphism | Dispatch form | Way of implementation |
---|---|---|
Ad hoc polymorphism | Static | Function or operator overloading |
Parametric polymorphism | Static | Generics |
Subtyping polymorphism | Dynamic | Inheritance |
Ad hoc polymorphism means that language allows to define multiple functions with the same name but with different signatures and definitions, i.e., every implementation of ad hoc function has the same name.
Compiler will dispatch every ad hoc function call with appropriate implementation depending on the type of argument to which this call is applied.
Ad hoc polymorphism is not part of the type system.
The same function Add has 2 different implementations for 2 types:
function Add(x, y : Integer) : Integer;
begin
Add := x + y
end;
function Add(s, t : String) : String;
begin
Add := Concat(s, t)
end;
begin
Writeln(Add(1, 2)); (* Prints "3" *)
Writeln(Add('Hello, ', 'Mammals!')); (* Prints "Hello, Mammals!" *)
end.
Parametric polymorphism means that language allows to define one function that can handle values of different types.
Parametric polymorphism implies that function does not depend on a specific type and has the same logic for every possible type.
Generic is a declaration/definition of type (function/struct/enum/trait) that contains type variable (aka type parameter).
Type var is often defined in angle brackets, e.g. <T>
or <E>
. There can be multiple type vars in generic.
Parametric polymorphism is a part of the type system.
fn f <T>(param: T) {}
Subtyping polymorphism is often implemented through inheritance in OOP languages.
Virtual method is a method that inheritable and may be overridden.
Virtual methods are dispatched dynamically.
A pure virtual method (aka abstract method) is a virtual method that has a declaration (signature) and no definition (implementation).
Abstract type (also class) is a type that contains at least one abstract method.
Abstract type cannot be instantiated.
Concrete type is a type that is not abstract.
Concrete type can be instantiated.
So, an abstract type may provide no implementation, or an incomplete implementation.
Interface (aka protocol, trait) is an abstract type whose methods are all abstract methods.