Co- and contra-variance (Lucian Wischik)


How do I convert an IEnumerable(Of CheckingAccount) into an IEnumerable(Of Account) ? (where CheckingAccount inherits from Account).

Currently this conversion isn’t allowed, but we’re considering a VB language feature to support it. The topic is called “co- and contravariance”, and has been in the CLR since 2005 or so, but no one’s yet released a .Net language that uses it. Eric Lippert has written a series of blog posts to explore how variance might appear in a hypothetical future version of C# and gather feedback, and I’m going to do the same for VB.


args As New List(Of ConstantExpression)
Dim y = Expression.Call(instance, method, args)

Here above is a practical need I had for variance last week. The problem is that “args” is a List(Of ConstantExpression), but the “Expression.Call” is expecting just an IEnumerable(Of Expression), and so we can’t do it. But it should really work! since the code is typesafe, and the CLR already supports this kind of conversion anyway.

What we have in mind is… well, it’s too long to fit in a blog post so I’ve written it up here: VB Variance.

Our goal in writing about this now is to get feedback on our designs from potential users. So please, add your comments!


Leave a comment

Feedback usabilla icon