<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Co- and contra-variance: how do I convert a List(Of Apple) into a List(Of Fruit)?</title><link>http://blogs.msdn.com/lucian/archive/2008/10/02/co-and-contra-variance-how-do-i-convert-a-list-of-apple-into-a-list-of-fruit.aspx</link><description>This is the first in a series of posts exploring how we might implement generic co- and contra-variance in a hypothetical future version of VB. This is not a promise about the next version of VB; it's just one possible proposal, written up here to get</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Only need have one key work</title><link>http://blogs.msdn.com/lucian/archive/2008/10/02/co-and-contra-variance-how-do-i-convert-a-list-of-apple-into-a-list-of-fruit.aspx#8974482</link><pubDate>Fri, 03 Oct 2008 00:45:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8974482</guid><dc:creator>ljw1004</dc:creator><description>&lt;p&gt;[posted by btcheng]&lt;/p&gt;
&lt;p&gt;It seems to me you only need to have one key work Like x as CLONE List(Of T) on function call. Then in the scope of the method x will reference clone of List(Of T). Everything works as normal. Modifications of list are bound into scope of the method or object and will not have effect callers list. Furthermore modifications to objects in the list will persist out of scope which is porsumably is a desired result. This would allow stuff like is. &lt;/p&gt;
&lt;p&gt;Class Fruits&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Public Property Washed() As Boolean&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Public Property Eaten() As Boolean&lt;/p&gt;
&lt;p&gt;End Class&lt;/p&gt;
&lt;p&gt;Class WashedFruitList&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Private mList As List(Of Fruits)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Public Sub New(ByVal NewList As List(Of Fruits))&lt;/p&gt;
&lt;p&gt; &amp;nbsp;For Each Fruit As Fruits In NewList&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Fruit.Washed = True&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Next&lt;/p&gt;
&lt;p&gt; &amp;nbsp;mList = NewList&lt;/p&gt;
&lt;p&gt; &amp;nbsp;End Sub&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Public Sub EatList()&lt;/p&gt;
&lt;p&gt; &amp;nbsp;For Each Fruit As Fruits In mList&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Fruit.Eaten = True&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Next&lt;/p&gt;
&lt;p&gt;mList=New List(Of Fruits)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;End Sub&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Public Sub Add(ByVal itm As Fruits)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;itm.Washed = True&lt;/p&gt;
&lt;p&gt; &amp;nbsp;mList.Add(itm)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;End Sub&lt;/p&gt;
&lt;p&gt;End Class&lt;/p&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;p&gt;Dim lApples As New List (Of Apples)&lt;/p&gt;
&lt;p&gt;Dim CartFriut as WashedFruitList&lt;/p&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;p&gt;CartFriut = New WashedFruitList(lApples)&lt;/p&gt;
&lt;p&gt;' All apples in lApples would be washed &lt;/p&gt;
&lt;p&gt;CartFriut.Add(BobsBanana)&lt;/p&gt;
&lt;p&gt;' lApples would only still have Apples&lt;/p&gt;
&lt;p&gt;lApples.Add(NewApple1)&lt;/p&gt;
&lt;p&gt;lApples.Add(NewApple2)&lt;/p&gt;
&lt;p&gt;CartFriut.EatList&lt;/p&gt;
&lt;p&gt;' LApples Still Exist Members and all members of LApples would be eaten except NewApple1, and NewApple2 &lt;/p&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;p&gt;. &lt;/p&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;p&gt;If you want further clarification email me @ btcheng@yahoo.com&lt;/p&gt;
</description></item><item><title>call-site variance</title><link>http://blogs.msdn.com/lucian/archive/2008/10/02/co-and-contra-variance-how-do-i-convert-a-list-of-apple-into-a-list-of-fruit.aspx#8974485</link><pubDate>Fri, 03 Oct 2008 00:46:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8974485</guid><dc:creator>ljw1004</dc:creator><description>&lt;p&gt;[posted by Anonymous]&lt;/p&gt;
&lt;p&gt;It seems to me that declaration-site variance (as implemented in the CLR, and now proposed for C# and VB.NET) is less useful than call-site variance (as implemented in Java using wildcards). Here's why: stop for a moment and think, how many of BCL generic classes could actually be converted to use this feature? There's IEnumerable&amp;lt;T&amp;gt;, alright - but what else? ICollection&amp;lt;T&amp;gt; is right out, because it is IEnumerable&amp;lt;T&amp;gt; (and hence covariant), but it also takes T as an argument for Add() (which is contravariant) - the intersection of those is invariant. The same goes for all interfaces further extending ICollection&amp;lt;T&amp;gt;, such as IList&amp;lt;T&amp;gt;.&lt;/p&gt;
&lt;p&gt;On the other hand, with wildcards, I can deal with ICollection&amp;lt;T&amp;gt; either in covariant or contravariant fashion: for example, only using GetEnumerator() and Count - a typical real-world scenario - is covariant; only using Add() is contravariant. You can do this with declaration-site variance too, but you have to split every interface into its covariant and contravariant parts, and I doubt the BCL team would do it at this point; besides, it relies upon interface designers to do the right thing, and gives the user no recourse if they don't.&lt;/p&gt;
</description></item><item><title>call-site variance</title><link>http://blogs.msdn.com/lucian/archive/2008/10/02/co-and-contra-variance-how-do-i-convert-a-list-of-apple-into-a-list-of-fruit.aspx#8974502</link><pubDate>Fri, 03 Oct 2008 00:55:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8974502</guid><dc:creator>ljw1004</dc:creator><description>&lt;p&gt;Agreed, declaration-site variance does rely on interface designers to do the right thing, and yes I too doubt the BCL team would do it at this point.&lt;/p&gt;
</description></item><item><title>re: Co- and contra-variance: how do I convert a List(Of Apple) into a List(Of Fruit)?</title><link>http://blogs.msdn.com/lucian/archive/2008/10/02/co-and-contra-variance-how-do-i-convert-a-list-of-apple-into-a-list-of-fruit.aspx#9001398</link><pubDate>Thu, 16 Oct 2008 06:59:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9001398</guid><dc:creator>Branco Medeiros</dc:creator><description>&lt;p&gt;This is good stuff!&lt;/p&gt;
&lt;p&gt;(Btw, welcome aboard! It's a pitty Paul Vicky is moving out, but I guess a fresh new pair of eyes over the language will be a good thing!)&lt;/p&gt;
&lt;p&gt;There were many times I felt the need of covariance (the Out thing), and I knew there was support for it in the CLI. Good to know it's on your radar. And the syntax seems to fit nicely (too bad parameter constness can't be enforced in the CLI, that would probably easy things out for covariance, I guess).&lt;/p&gt;
&lt;p&gt;Now there are a few other things the CLI provides that VB could actually have: managed pointers, method Jumping and (ta-dah!) custom array lower bounds =)))&lt;/p&gt;
&lt;p&gt;Congratulations for the very interesting post.&lt;/p&gt;
&lt;p&gt;Branco.&lt;/p&gt;
</description></item></channel></rss>