Publications » Variant Parametric Types: A Flexible Subtyping Scheme for Generics

Variant Parametric Types: A Flexible Subtyping Scheme for Generics

Atsushi Igarashi, Mirko Viroli
We develop the mechanism of variant parametric types as a means to enhance synergy between parametric and inclusion polymorphism in object-oriented programming languages. Variant parametric types are used to control both the subtyping between different instantiations of one generic class and the accessibility of their fields and methods. On one hand, one parametric class can be used to derive covariant types, contravariant types, and bivariant types (generally called variant parametric types) by attaching a variance annotation to a type argument. On the other hand, the type system prohibits certain method/field accesses, according to variance annotations, when these accesses may otherwise make the program unsafe. By exploiting variant parametric types, a programmer can write generic code abstractions that work on a wide range of parametric types in a safe manner. For instance, a method that only reads the elements of a container of numbers can be easily modified so as to accept containers of integers, floating-point numbers, or any subtype of the number type.Technical subtleties in typing for the proposed mechanism are addressed in terms of an intuitive correspondence between variant parametric and bounded existential types. Then, for a rigorous argument of correctness of the proposed typing rules, we extend Featherweight GJ—an existing formal core calculus for Java with generics—with variant parametric types and prove type soundness.
ACM Transactions on Programming Languages and Systems 28(5), pages 795-847, September 2006, ACM Press, New York, NY, USA
@article{generics-toplas28,
	Address = {New York, NY, USA},
	Author = {Igarashi, Atsushi and Viroli, Mirko},
	Doi = {10.1145/1152649.1152650},
	Issn = {0164-0925},
	Journal = {ACM Transactions on Programming Languages and Systems},
	Month = sep,
	Number = 5,
	Pages = {795--847},
	Publisher = {ACM Press},
	Title = {Variant Parametric Types: A Flexible Subtyping Scheme for Generics},
	Volume = 28,
	Year = 2006}