4.7 F-Bounded Polymorphism
In a singly-dispatched language with F-bounded polymorphism, the comparable type could be defined as follows:
Then comparable types could be declared as subtypes of instances of this parameterized type:class
comparable[T] {signature
=(y:T);method !=(y:
T):bool {
not(x = y) }
signature < (y:
T):bool; method <=(y:
T):bool {
x = y | x < y }method >=(y:
T):bool {
x = y | x > y }method > (y:
T):bool {
y < x } };
Functions that are polymorphic over comparable types could be written using explicit parameterization as follows:extend
numisa
comparable[num];extend
collection['T <= comparable[T]]isa
comparable[collection[T]];
method
min[T <= comparable[T]](x1:T, x2:T):T {
if (x1 < x2, { x1 }, { x2 })}
The seemingly recursive nature of the explicit type parameter of both collection
and min
is not a problem. For a type of the form 'T
<=
type[T]
, first the actual instantiating type parameter is bound to the type variable T, then this type is checked against type instantiated with the type bound to T. For example, for the following call:
min[num](3, 4.5)first
T
is bound to num
, then the system checks that num
is a subtype of comparable[num]
, which holds.
Generated with Harlequin WebMaker