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

4.5 Related Work

4.5.2 Languages Based on SelfType or Matching

Some languages provide only restricted forms of F-bounded polymorphism. In TOOPLE [Bruce et al. 93] and Strongtalk [Bracha & Griswold 93], a special type SelfType is introduced, which can be used as the type of method arguments, results, or variables; roughly speaking, a class C with references to SelfType can be modeled with the F-bounded declaration

forall SelfType where SelfType <= C[SelfType]:
 template object C[SelfType];

SelfType supports binary methods like <= and methods like copy that return values of exactly the same type as their receiver, but it does not support other kinds of F-bounded parameterization. Other languages provide a related notion called matching, which allows a kind of F-bounded polymorphism where a single type variable is bounded by a function of itself (but of no other type variables); languages with matching include PolyTOIL [Bruce et al. 95b] and LOOM [Bruce et al. 97]. The key advantage of SelfType and matching is convenient syntactic support for a common idiom, but it is less powerful than F-bounded polymorphism. Additionally, the LOOM language drops subtyping altogether in favor of matching, which costs it the ability to support run-time mixing of values of different classes but common supertypes, such as performing binary operations on the elements of a list of mixed integers and floats. SelfType and matching also are weaker than F-bounded polymorphism in that they force subclasses to continually track the more specific type; they cannot stop narrowing at some subclass and switch to normal subtyping below that point. For example, with F-bounded polymorphism, the parameterized ordered type can have its type parameter "narrowed" and then fixed (say at ordered[num]), allowing subtypes of the fixed type (such as int and float) to be freely mixed. This open/closed distinction for recursive references to a type was noted previously by Eifrig et al. [Eifrig et al. 94].