Variance and Generalized Constraints for C# Generics

Generic types in C$^{\sharp}$ behave invariantly with respect to subtyping. We propose a system of type-safe variance for C$^{\sharp}$ that supports the declaration of covariant and contravariant type parameters on generic types. To support more widespread application of variance we also generalize the existing constraint mechanism with arbitrary subtype assertions on classes and methods. This extension is useful even in the absence of variance, and subsumes equational constraints proposed for Generalized Algebraic Data Types (GADTs). We formalize the subtype relation in both declarative and syntax-directed style, and describe and prove the correctness of algorithms for constraint closure and subtyping. Finally, we formalize and prove a type safety theorem for a featherweight language with variant classes and generalized constraints.

[1]  Nicu G. Fruja Type Safety of Generics for the .NET Common Language Runtime , 2006, ESOP.

[2]  Andrew Kennedy,et al.  Transposing F to C♯: expressivity of parametric polymorphism in an object‐oriented language , 2004, Concurr. Pract. Exp..

[3]  Benjamin C. Pierce,et al.  Bounded Quantification is Undecidable , 1994, Inf. Comput..

[4]  Mirko Viroli,et al.  Parametric polymorphism in Java: an approach to translation based on reflective features , 2000, OOPSLA '00.

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

[6]  Philip Wadler,et al.  Featherweight Java: a minimal core calculus for Java and GJ , 2001, TOPL.

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

[8]  Mirko Viroli,et al.  Parametric Polymorphism in Java through the Homogeneous Translation LM: Gathering Type Descriptors a , 2000 .

[9]  Scott F. Smith,et al.  Subtyping Constrained Types , 1996, SAS.

[10]  Giuseppe Castagna,et al.  Decidable bounded quantification , 1994, POPL '94.

[11]  Andrew Kennedy,et al.  Generalized algebraic data types and object-oriented programming , 2005, OOPSLA '05.

[12]  François Pottier,et al.  Simplifying Subtyping Constraints: A Theory , 2001, Inf. Comput..

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

[14]  Martin Odersky,et al.  Scalable component abstractions , 2005, OOPSLA '05.

[15]  Peter Sestoft,et al.  C# precisely , 2004 .