Taming the wildcards: combining definition- and use-site variance

Variance allows the safe integration of parametric and subtype polymorphism. Two flavors of variance, definition-site versus use-site variance, have been studied and have had their merits hotly debated. Definition-site variance (as in Scala and C#) offers simple type-instantiation rules, but causes fractured definitions of naturally invariant classes; Use-site variance (as in Java) offers simplicity in class definitions, yet complex type-instantiation rules that elude most programmers. We present a unifying framework for reasoning about variance. Our framework is quite simple and entirely denotational, that is, it evokes directly the definition of variance with a small core calculus that does not depend on specific type systems. This general framework can have multiple applications to combine the best of both worlds: for instance, it can be used to add use-site variance annotations to the Scala type system. We show one such application in detail: we extend the Java type system with a mechanism that modularly infers the definition-site variance of type parameters, while allowing use-site variance annotations on any type-instantiation. Applying our technique to six Java generic libraries (including the Java core library) shows that 20-58 (depending on the library) of generic definitions are inferred to have single-variance; 8-63% of method signatures can be relaxed through this inference, and up to 91% of existing wildcard annotations are unnecessary and can be elided.

[1]  Sophia Drossopoulou,et al.  A Model for Java with Wildcards , 2008, ECOOP.

[2]  Mads Torgersen,et al.  Unifying Genericity - Combining the Benefits of Virtual Types and Parameterized Classes , 1999, ECOOP.

[3]  Gilad Bracha,et al.  Strongtalk: typechecking Smalltalk in a production environment , 1993, OOPSLA '93.

[4]  William R. Cook,et al.  A Proposal for Making Eiffel Type-Safe , 1989, Comput. J..

[5]  Frank Tip,et al.  Refactoring for Parameterizing Java Classes , 2007, 29th International Conference on Software Engineering (ICSE'07).

[6]  Gilad Bracha,et al.  Adding wildcards to the Java programming language , 2004, SAC '04.

[7]  Shan Shan Huang,et al.  cJ: enhancing java with safe type conditions , 2007, AOSD.

[8]  Mirko Viroli,et al.  Variant parametric types: A flexible subtyping scheme for generics , 2006, TOPL.

[9]  Andrew Kennedy,et al.  Transposing F to C s : expressivity of parametric polymorphism in an object-oriented language: Research Articles , 2004 .

[10]  Pierre America,et al.  A parallel object-oriented language with inheritance and subtyping , 1990, OOPSLA/ECOOP '90.

[11]  Robert Cartwright,et al.  Compatible genericity with run-time types for the Java programming language , 1998, OOPSLA '98.

[12]  Dachuan Yu,et al.  Variance and Generalized Constraints for C# Generics , 2006, ECOOP.

[13]  Peter H. Golde,et al.  C# Language Specification , 2003 .

[14]  Andrew C. Myers,et al.  Subtypes vs. where clauses: constraining parametric polymorphism , 1995, OOPSLA.