[Next] [Previous] [Up] [Top] [Contents] [Index]

4.7 F-Bounded Polymorphism

4.7.1 Motivation

In section 4.2, the 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:

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 }
Numbers could be declared to be comparable as follows:

extend number isa comparable;
With this declaration, any pair of numbers could be used as arguments to the min method. We would also like to state that collections of comparable things are also comparable:

extend collection['T <= comparable] isa 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 declaration '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.


The Cecil Language: Specification and Rationale, Version 2.1 - 25 MARCH 1997
[Next] [Previous] [Up] [Top] [Contents] [Index]

Generated with Harlequin WebMaker