InfoQ

News

Covariance and Contravariance in .NET Generics

Posted by Jonathan Allen on Aug 13, 2008 06:24 AM

Community
.NET
Topics
Programming
Tags
Visual Basic.NET ,
Generics ,
C#

Currently .NET languages such as VB and C# do not support covariance and contravariance for generics. This is not likely to chance in the near future, though people at Microsoft are talking about it. A full introduction to covariance and contravariance would take too long. For that we ask reader's turn to Eric Lippert's series on Covariance and Contravariance in C#.

In order to add support for covariant and contravariant generic types in VB, Lucian Wischik has proposed the following syntax.

Type parameters can be decorated with the keywords "In" and "Out. An "In" type is a type that can only appear as a method parameter. Likewise, an "Out" type is one that would only appear as the return type of a method.

An example of where Out types would be useful is IEnumerable(Of T). If a function is expecting an IEnumerable(Of Animal), giving it an IEnumerable(of Bird) should work under all circumstances. For In types, a somewhat contrived example is in order. Consider this interface:

Interface IWriter(Of T)
    Write(value As T)

If you pass an IWriter(Of Bird) to a function expecting an IWriter(Of Animal), clearly it would not work. The method could pass any subclass of Animal to IWriter.Write, though only Birds are supported.

With the annotations, the interfaces would look like:

Interface IEnumerable(Of Out T)

Interface IWriter(Of In T)

While this was proposed for VB, it could also be used in C#.

interface IEnumerable<out T>

interface IWriter<in T>

Unfortunately this syntax doesn't directly work in the most common scenarios. Consider IList(Of T). When passing to methods that write to the collection, T should be an In type. But when passing to methods that read from the collection, it should be an Out type. Perhaps the solution here is to retroactively create base classes for IList that separate the methods that accept a T value from the ones that return one.

On a historical note, C# and VB both support array covariance (out/IEnumerable scenario) even though it can lead to runtime errors in contravariant situations (in/IWriter scenario). This was done in order to make C# more compatible with Java. This is generally considered a poor decision, but it cannot be undone at this time.

No comments

Watch Thread Reply

Educational Content

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.