4.7 F-Bounded Polymorphism
min
method was defined as follows:
method
min(x1:'T1 <= comparable, x2:'T2 <= comparable):T1|T2 {
if (x1 < x2, { x1 }, { x2 })}
The type comparable
might be defined as follows:
Numbers could be declared to be comparable as follows:abstract object
comparable;signature = (x@:comparable, y@:comparable):bool; method !=(x@:comparable, y@:comparable):bool {
not(x = y) }
signature < (x@:comparable, y@:comparable):bool; method <=(x@:comparable, y@:comparable):bool {
x = y | x < y }method >=(x@:comparable, y@:comparable):bool {
x = y | x > y }method > (x@:comparable, y@:comparable):bool {
y < x }
With this declaration, any pair of numbers could be used as arguments to theextend
numberisa
comparable;
min
method. We would also like to state that collections of comparable things are also comparable:
Unfortunately, these declarations are not likely to both appear in the same Cecil program, because this would require that numbers could be compared against collections of numbers. Subtyping as used in the declarationextend
collection['T <= comparable]isa
comparable;
'T
<=
comparable
in the min
method only constrains a single object. What we need to do for this case is to be able to describe that two objects come from related types, e.g., that both arguments to min
are subtypes of number
or that both are subtypes of the collection type instantiated with related types.
Generated with Harlequin WebMaker