<?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>Immutability in C# Part Three: A Covariant Immutable Stack</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/12/06/immutability-in-c-part-three-a-covariant-immutable-stack.aspx</link><description>Now suppose we had a hypothetical future version of C# in which interface covariance worked, and we wanted a covariant immutable stack. That is, we want to be able to implicitly convert an IStack&amp;lt;Giraffe&amp;gt; to IStack&amp;lt;Mammal&amp;gt; . As we've already</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>WAW!!! What a serie of blog posts for "Covariance and Contravariance" - C# Future...</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/12/06/immutability-in-c-part-three-a-covariant-immutable-stack.aspx#9236175</link><pubDate>Thu, 18 Dec 2008 16:22:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9236175</guid><dc:creator>Journal of Abu Sayed Mohammad Ismail</dc:creator><description>&lt;p&gt;So nicely step by step blogged by Eric Lippert for &amp;amp;quot;Covariance and Contravariance&amp;amp;quot; as &amp;amp;quot;Fabulous&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9236175" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Three: A Covariant Immutable Stack</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/12/06/immutability-in-c-part-three-a-covariant-immutable-stack.aspx#9003448</link><pubDate>Fri, 17 Oct 2008 21:16:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9003448</guid><dc:creator>Rob Paveza</dc:creator><description>&lt;p&gt;Hi Eric,&lt;/p&gt;
&lt;p&gt;I've just read through pretty much your entire series on covariance and contravariance. &amp;nbsp;I'd certainly love to see support for it (and it may start making its way into my blog series on my C# wishlist), but I have to say: I don't like the example in this blog post.&lt;/p&gt;
&lt;p&gt;Above all, I don't like that we've created a new imaginary interface IStack&amp;lt;+T&amp;gt; that has every operation for a stack *except* for Push(). &amp;nbsp;A Stack is a well-known and well-understood data structure - they even talked about it at university! - and to create an interface, which models behavior, but to not include all of the behavior for the explicit purpose of supporting a language feature seems wrong. &amp;nbsp;It feels like coincidental cohesion.&lt;/p&gt;
&lt;p&gt;I also have a substantial problem with the use of an extension method to implement Push(). &amp;nbsp;I've mentioned this in at least two talks I've presented on C# 3.0 and extension methods, not to mention my blog: Extension methods allow developers to feel like they have a twisted kind of multiple inheritance, except that it's not MI at all because there's no virtual method. &amp;nbsp;The compiler, and consequently the runtime, binds to the extension method unless it can explicitly resolve the method on the defining class or interface, but even if the real implementation class was Stack&amp;lt;T&amp;gt; which implements Push(), but the variable is declared as IStack&amp;lt;+T&amp;gt;, we'll use the extension method. &amp;nbsp;This is a scary thought when you consider applying this solution to non-data-structure classes with virtual methods that are no longer making virtual calls.&lt;/p&gt;
&lt;p&gt;Now, to qualify: I've never said that extension methods are bad; I've simply said that software developers need to be cognizant of the fact that they might not always be calling what they think they're calling. &amp;nbsp;IntelliSense and the compiler make it feel like we're operating on a real behavior of objects.&lt;/p&gt;
&lt;p&gt;FWIW, I saw someone else post syntax such as:&lt;/p&gt;
&lt;p&gt;delegate U Action&amp;lt;* is T, U is *&amp;lt;(T item);&lt;/p&gt;
&lt;p&gt;I think that's fantastic syntax. :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9003448" width="1" height="1"&gt;</description></item><item><title>Immutability in C#</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/12/06/immutability-in-c-part-three-a-covariant-immutable-stack.aspx#7136552</link><pubDate>Thu, 17 Jan 2008 02:45:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7136552</guid><dc:creator>Community Blogs</dc:creator><description>&lt;p&gt;For some reason, there's been a lot of buzz lately around immutability in C#. If you're interested in&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7136552" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Three: A Covariant Immutable Stack</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/12/06/immutability-in-c-part-three-a-covariant-immutable-stack.aspx#6809178</link><pubDate>Wed, 19 Dec 2007 22:44:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6809178</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;The point of this was that IF we add covariance to the language in some future version, THEN this would work. We have not yet added covariance, so this does not work.&lt;/p&gt;
&lt;p&gt;See my recent ten part series on covariance for more details.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6809178" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Three: A Covariant Immutable Stack</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/12/06/immutability-in-c-part-three-a-covariant-immutable-stack.aspx#6803094</link><pubDate>Wed, 19 Dec 2007 09:34:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6803094</guid><dc:creator>cipto</dc:creator><description>&lt;p&gt;Hi, em i try ur code&lt;/p&gt;
&lt;p&gt;Where to put this code below,so the code can work:&lt;/p&gt;
&lt;p&gt;public static class Extensions&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static IStack&amp;lt;T&amp;gt; Push&amp;lt;T&amp;gt;(this IStack&amp;lt;T&amp;gt; s, T t)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;{ &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return Stack&amp;lt;T&amp;gt;.Push(t, s);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;IStack&amp;lt;+T&amp;gt; , + is unexpected token&lt;/p&gt;
&lt;p&gt;how come + generate error&lt;/p&gt;
&lt;p&gt;is this for .net 2.0 or not?&lt;/p&gt;
&lt;p&gt;Please give me a complete listing code, so i can follow this one, &lt;/p&gt;
&lt;p&gt;i've followed your part one and two and success.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6803094" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Three: A Covariant Immutable Stack</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/12/06/immutability-in-c-part-three-a-covariant-immutable-stack.aspx#6764599</link><pubDate>Thu, 13 Dec 2007 23:20:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6764599</guid><dc:creator>David</dc:creator><description>&lt;p&gt;So you did :) Looks like I have a lot more reading to do.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6764599" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Three: A Covariant Immutable Stack</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/12/06/immutability-in-c-part-three-a-covariant-immutable-stack.aspx#6752944</link><pubDate>Thu, 13 Dec 2007 02:54:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6752944</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Yes. I just wrote a ten part series of blog posts on exactly that subject.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6752944" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Three: A Covariant Immutable Stack</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/12/06/immutability-in-c-part-three-a-covariant-immutable-stack.aspx#6752699</link><pubDate>Thu, 13 Dec 2007 02:29:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6752699</guid><dc:creator>David</dc:creator><description>&lt;p&gt;Will the hypothetical covariant C# have a way of enforcing covariantability (is that a word)? For example, would it be possible to add a &amp;quot;covariant&amp;quot; keyword to a type definition, and then have the compiler enforce that all operations in that type are covariant?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6752699" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Three: A Covariant Immutable Stack</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/12/06/immutability-in-c-part-three-a-covariant-immutable-stack.aspx#6691280</link><pubDate>Fri, 07 Dec 2007 13:05:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6691280</guid><dc:creator>Gilles Michard</dc:creator><description>&lt;p&gt;Is there a hope that anonymous types { Ta a, Tb b} would implement the covariant interface &lt;/p&gt;
&lt;p&gt;interface IAnonymous_a_b&amp;lt;+Ta,+Tb&amp;gt;{&lt;/p&gt;
&lt;p&gt;Ta a { get;}&lt;/p&gt;
&lt;p&gt;Tb b { get;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Combined with an automatic type inference (most derived compatible type) , it would really help with Enumerable.Concat when using different types (casting to an anonymous type is not possible)&lt;/p&gt;
&lt;p&gt;Perhaps VB will do that if not C#?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6691280" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Three: A Covariant Immutable Stack</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/12/06/immutability-in-c-part-three-a-covariant-immutable-stack.aspx#6690034</link><pubDate>Fri, 07 Dec 2007 10:18:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6690034</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;&amp;gt; IStack&amp;lt;Mammal&amp;gt; sm4 = sm3.Push(new Tiger(&amp;quot;Tony&amp;quot;)) is actually &lt;/p&gt;
&lt;p&gt;&amp;gt; IStack&amp;lt;Mammal&amp;gt; sm4 = sm3.Push&amp;lt;Tiger&amp;gt;(new Tiger(&amp;quot;Tony&amp;quot;)) &lt;/p&gt;
&lt;p&gt;&amp;gt; because the type is inferred from from type of the parameter that the method is called with&lt;/p&gt;
&lt;p&gt;Nope. &amp;nbsp;Did you actually try it? (Obviously the covariant conversion will not work, but this call will.)&lt;/p&gt;
&lt;p&gt;Hint: the type argument for the generic method is inferred from more than just the &amp;quot;new Tiger&amp;quot; parameter.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6690034" width="1" height="1"&gt;</description></item></channel></rss>