{"id":4213,"date":"2008-08-07T16:52:00","date_gmt":"2008-08-07T16:52:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2008\/08\/07\/co-and-contra-variance-lucian-wischik\/"},"modified":"2024-07-05T14:20:42","modified_gmt":"2024-07-05T21:20:42","slug":"co-and-contra-variance-lucian-wischik","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/co-and-contra-variance-lucian-wischik\/","title":{"rendered":"Co- and contra-variance (Lucian Wischik)"},"content":{"rendered":"<p>How do I convert an <strong>IEnumerable(Of CheckingAccount)<\/strong> into an <strong>IEnumerable(Of Account)<\/strong> ? (where CheckingAccount inherits from Account).<\/p>\n<p>Currently this conversion isn&#8217;t allowed, but we&#8217;re considering a VB language feature to support it. The topic is called &#8220;co- and contravariance&#8221;, and has been in the CLR since 2005 or so, but no one&#8217;s yet released a .Net language that uses it. Eric Lippert has written a <a class=\"\" href=\"http:\/\/blogs.msdn.com\/ericlippert\/archive\/tags\/Covariance+and+Contravariance\/default.aspx\">series of blog posts<\/a>&nbsp;to explore how&nbsp;variance might appear in a hypothetical future version of C# and gather feedback, and I&#8217;m going&nbsp;to do the same for VB.<\/p>\n<p><font color=\"#0000ff\" size=\"2\"><font color=\"#0000ff\" size=\"2\"><\/p>\n<p>Dim<\/p>\n<p><\/font><\/font><font size=\"2\"> args <\/font><font color=\"#0000ff\" size=\"2\"><font color=\"#0000ff\" size=\"2\">As<\/font><\/font><font size=\"2\"> <\/font><font color=\"#0000ff\" size=\"2\"><font color=\"#0000ff\" size=\"2\">New<\/font><\/font><font size=\"2\"> List(<\/font><font color=\"#0000ff\" size=\"2\"><font color=\"#0000ff\" size=\"2\">Of<\/font><\/font><font size=\"2\"> ConstantExpression)<br \/>args.Add(Expression.Constant(2))<br \/>args.Add(Expression.Constant(3))<br \/><\/font><font color=\"#0000ff\" size=\"2\"><font color=\"#0000ff\" size=\"2\">Dim<\/font><\/font><font size=\"2\"> y = Expression.Call(instance, method, <u><strong>args<\/strong><\/u>)<\/font><font size=\"2\"><\/p>\n<p>Here above is&nbsp;a practical need I had for variance last week. The problem is that &#8220;args&#8221; is a List(Of ConstantExpression), but the &#8220;Expression.Call&#8221; is expecting just an IEnumerable(Of Expression), and so we can&#8217;t do it.&nbsp;But it should really work! since&nbsp;the code is&nbsp;typesafe, and the CLR already supports this kind of conversion anyway.<\/p>\n<p>What we have in mind is&#8230; well, it&#8217;s too long to fit in a blog post so I&#8217;ve written it up here: <a class=\"\" href=\"http:\/\/www.wischik.com\/lu\/programmer\/vbdev\/variance-introduction.html\">VB Variance<\/a>.<\/p>\n<p>Our goal in writing&nbsp;about this now&nbsp;is to get feedback on our designs from potential users. So please, add your comments!<\/p>\n<p><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How do I convert an IEnumerable(Of CheckingAccount) into an IEnumerable(Of Account) ? (where CheckingAccount inherits from Account). Currently this conversion isn&#8217;t allowed, but we&#8217;re considering a VB language feature to support it. The topic is called &#8220;co- and contravariance&#8221;, and has been in the CLR since 2005 or so, but no one&#8217;s yet released a [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[21,195],"tags":[99],"class_list":["post-4213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-im-a-vb","category-visual-basic","tag-lucian-wischik"],"acf":[],"blog_post_summary":"<p>How do I convert an IEnumerable(Of CheckingAccount) into an IEnumerable(Of Account) ? (where CheckingAccount inherits from Account). Currently this conversion isn&#8217;t allowed, but we&#8217;re considering a VB language feature to support it. The topic is called &#8220;co- and contravariance&#8221;, and has been in the CLR since 2005 or so, but no one&#8217;s yet released a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/4213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=4213"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/4213\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=4213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=4213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=4213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}