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

4 Parameterization and Parametric Polymorphism

4.5 Parameterized Objects and Method Lookup

A method can be attached either to a parameterized object itself, to a group of instances of the parameterized object, or to a single instance. The form of the instantiating type pattern determines the scope of the method implementation. For example, the following method is attached to a parameterized object (i.e., to all instances of the parameterized object), since the parameter to the parameterized object is universally quantified:

method length(c@:cons['T]):int { ... }
To attach a method to a subset of the instances, a bounded implicit type variable is used as the instantiating parameter:

method hash(c@:cons['T <= hashable]):int { ... }
If the instantiating type contains no implicit type parameter bindings, then the method is attached to a particular instance of the parameterized object:

method detabify(c@:cons[char]):cons[char] { ... }
If multiple method implementations with the same name are defined on the same parameterized object, but with different degrees of quantification:

method print(c@:collection['T]):void { ... }
method print(c@:collection['T <= printable]):void { ... }
method print(c@:collection['T <= hashable]):void { ... }
method print(c@:collection[char]):void { ... }
then the issue of which method to invoke arises. One reasonable choice would be to declare such redundant method implementations ambiguous: more than one print method of one argument is defined for collections of characters. An alternative semantics would be to choose the most-specific matching method implementation, where a method is more specific than another if it is associated with a subset of the instances of the other. We have little experience with attaching multiple versions of the same method to different subsets of a parameterized types, so initially we have selected the more-conservative position of allowing only a single version of a method to be provided for any given instance of a parameterized type; the above declarations are illegal.


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

Generated with Harlequin WebMaker