<?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>Covariance and Contravariance in C#, Part Six: Interface Variance</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx</link><description>Over the last few posts I’ve discussed how it is possible to treat a delegate as contravariant in its arguments and covariant in its return type. A delegate is basically just an object which represents a single function call; we can do this same kind</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Covariance and Contravariance in C#, Part Six: Interface Variance</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx#5693757</link><pubDate>Fri, 26 Oct 2007 20:33:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5693757</guid><dc:creator>Bryan Watts</dc:creator><description>&lt;p&gt;Would the behavior surrounding variant types change based on the +/- modifier? Or does that just make it more explicit to the reader?&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C#, Part Six: Interface Variance</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx#5693974</link><pubDate>Fri, 26 Oct 2007 20:50:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5693974</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;The former. As I will discuss in my next post, &amp;nbsp;we need a way to say &amp;quot;this is covariant, this is invariant&amp;quot;, etc. We will not be able to deduce the desired variance just from the interface definition, so the default will still be &amp;quot;invariant&amp;quot;.&lt;/p&gt;
&lt;p&gt;Uh, hypothetically, of course.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C#, Part Six: Interface Variance</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx#5695314</link><pubDate>Fri, 26 Oct 2007 22:48:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5695314</guid><dc:creator>Bryan Watts</dc:creator><description>&lt;p&gt;Is it a binary behavior, i.e. could we collapse it to a single token specifying &amp;quot;this is variant in the only way that makes sense here&amp;quot;? Hypothetically.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C#, Part Six: Interface Variance</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx#5695599</link><pubDate>Fri, 26 Oct 2007 23:40:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5695599</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;An interesting idea -- however, as we'll see in my next post, the variance of one type parameter may depend upon the variance of another. There often is not one single &amp;quot;only way it makes sense here&amp;quot;. There are often multiple ways that it could work, and we'd then have to choose.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C#, Part Six: Interface Variance</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx#5707246</link><pubDate>Sat, 27 Oct 2007 12:18:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5707246</guid><dc:creator>Stefan Wenig</dc:creator><description>&lt;p&gt;I think one has to grasp the difference between covariance and contravariance to make any good use of it. &lt;/p&gt;
&lt;p&gt;Guessing variance from the interface would be dangerous even if you could do it, because the interface might change later, and this should not hurt existing code by removing or changing variance. The developer of the interface has to make it clear that he wants variance, so he will also be aware that certain changes might not be possible later (or would be breaking).&lt;/p&gt;
&lt;p&gt;During this blog series, I got used pretty well to the +/- notation, so why not. On the other hand, something more verbose like IEnumerable&amp;lt;covariant T&amp;gt; would not hurt either and be more c#-like. It's in declarations only anway.&lt;/p&gt;
&lt;p&gt;Would this, hypothetically, be for interfaces only, or can we have it for generic reference types too? &lt;/p&gt;
&lt;p&gt;This would be especially great because it would put an end to the &amp;quot;no common base type&amp;quot; problem of generics (well, not for value types), so instead of doing perverse reflection calls, we could just rely on &lt;/p&gt;
&lt;p&gt;object x = new List&amp;lt;string&amp;gt;();&lt;/p&gt;
&lt;p&gt;Assert (x is IEnumerable&amp;lt;object&amp;gt;)&lt;/p&gt;
&lt;p&gt;(can't think of an example with classes right now, but I know I wanted it on several occations in the past)&lt;/p&gt;
&lt;p&gt;(one could argue that another way to approach this problem would be to clean up the mess that generics made of the reflection API. probably not easy, but definately due!)&lt;/p&gt;
&lt;p&gt;can't wait. what's the hypothetical timeline? ;-)&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C#, Part Six: Interface Variance</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx#5712338</link><pubDate>Sat, 27 Oct 2007 17:32:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5712338</guid><dc:creator>Kyle Lahnakoski</dc:creator><description>&lt;p&gt;Would it be wrong to just avoid the covariant and contravariant checks? &amp;nbsp;With the complications you are showing, I think it is obvious that the parameter in a parametric type is orthogonal to the types of objects managed by that type.&lt;/p&gt;
&lt;p&gt;The illusion of contravariant only appears when you ASSUME the domain you assign a method is the ONLY domain the method can act on. &amp;nbsp;This assumption does not happen in all languages; a Javascript function can be designed to take a String, but can be passed any non-String. &amp;nbsp;The output of such a method is well defined: it outputs an exception. &amp;nbsp;With exceptions being accepted as part of a method's codomain, and realizing that all methods are well defined for all objects, then the issue of &amp;quot;contravariant on input parameters&amp;quot; disappears because all methods act on the same domain (everything).&lt;/p&gt;
&lt;p&gt;I believe little is lost in terms of optimization. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Object[] o=new Object[];&lt;/p&gt;
&lt;p&gt;	String[] s=o;	//Allowed at compile time&lt;/p&gt;
&lt;p&gt;Assigning arrays, or any collection, can be checked at runtime when the contravariant relationship is broken. &amp;nbsp;But this can be done at the collection level, not at the individual element level, as long as there is no type-erasure. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Same for function delegates; checks can be done at runtime. &amp;nbsp;But I see no reason why simple constant propagation (constant TYPE propagation) would catch most illegal maneuvers at compile time anyway.&lt;/p&gt;
&lt;p&gt;Furthermore, method inheritance must be contravariant on the parameter types to be logically consistent in an OO framework. &amp;nbsp;And this works by delegating out-of-domain parameters back to the super class. &amp;nbsp;I believe the logic in such a proof can be mimicked with some kind of recursive function delegate; proving &amp;quot;contravariant on parameter types&amp;quot; is logically inconsistent in a function delegate framework. &amp;nbsp;But that is only a strong feeling.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C#, Part Six: Interface Variance</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx#5716813</link><pubDate>Sat, 27 Oct 2007 21:28:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5716813</guid><dc:creator>Stefan Wenig</dc:creator><description>&lt;p&gt;Yeah, why bother with variance, haven't they heard of duck typing yet? Come on ... ;-)&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C#, Part Six: Interface Variance</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx#5753293</link><pubDate>Mon, 29 Oct 2007 05:48:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5753293</guid><dc:creator>Ryan</dc:creator><description>&lt;p&gt;I would love to have this kind of variance support.&lt;/p&gt;
&lt;p&gt;All too often I need to drop down to the IL level to do this kind of work.&lt;/p&gt;
&lt;p&gt;Thus, I am accustomed to the +- notation.&lt;/p&gt;
&lt;p&gt;However, perhaps your next post will have compelling reasons for another notation.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C#, Part Six: Interface Variance</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx#5757652</link><pubDate>Mon, 29 Oct 2007 11:00:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5757652</guid><dc:creator>Kyle Lahnakoski</dc:creator><description>&lt;p&gt;Stefan Wenig, I am not suggesting duck typing, I am suggesting strong dynamic typing of type parameters.&lt;/p&gt;
&lt;p&gt;Correction: &amp;nbsp;&amp;quot; Furthermore, method inheritance must be COVARIANT on the parameter types to be logically consistent in an OO framework.&amp;quot;&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C#, Part Six: Interface Variance</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx#5758099</link><pubDate>Mon, 29 Oct 2007 11:46:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5758099</guid><dc:creator>Stefan Wenig</dc:creator><description>&lt;p&gt;Kyle,&lt;/p&gt;
&lt;p&gt;in this case I don't understand what you're proposing. From your reference to JavaScript I just assumed you were talking about duck typing, which is the only way I know that can do this trick. If you're thinking about something else, is this anything that exists anywhere yet? Mind to post a link? Or is it a new idea you came up with? In this case, I'd ask you to elaborate. &lt;/p&gt;
&lt;p&gt;Which types should be compatible at compile time? Base&amp;lt;anything&amp;gt; and Base&amp;lt;anything else&amp;gt;? Or even Base&amp;lt;anything&amp;gt; and Derived&amp;lt;anything else&amp;gt;? Or maybe even Sibling1&amp;lt;anything&amp;gt; and Sibling2&amp;lt;anything ese&amp;gt;, given they both derive from Base&amp;lt;T&amp;gt;?&lt;/p&gt;
&lt;p&gt;How exactly does this translate to delegates? What do you mean by &amp;quot; Furthermore, method inheritance must be COVARIANT on the parameter types to be logically consistent in an OO framework.&amp;quot;? The this parameter? any parameter? why?&lt;/p&gt;
&lt;p&gt;Which kind of checks would have to be made at which point? Would you expect this to work for value type parameters too?&lt;/p&gt;
&lt;p&gt;Sounds like this would hurt my brain even more than explicit co/contravariance, but maybe I'm just not getting it. &lt;/p&gt;
&lt;p&gt;Stefan&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C#, Part Six: Interface Variance</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx#5764924</link><pubDate>Mon, 29 Oct 2007 16:44:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5764924</guid><dc:creator>Gilles Michard</dc:creator><description>&lt;p&gt;To go back to a remark in one of the first posts:&lt;/p&gt;
&lt;p&gt;What we strongly lack is overriding a read only property by a more specialized one.&lt;/p&gt;
&lt;p&gt;More generically it would be convenient to define an overload of a method and to have some &amp;quot;OverrideAttribute(typeof(string))&amp;quot; to say e.g. &amp;nbsp;string Compute(object o) is ovverriding the base class implementation of object Compute(string s).&lt;/p&gt;
&lt;p&gt;In C# it could be expressed as the 'generic keyword' &amp;quot;override&amp;lt;string&amp;gt;&amp;quot;.&lt;/p&gt;
&lt;p&gt;Covariance on return types woul be a gift, and covariance of IEnumerable&amp;lt;T&amp;gt; would definitely ease our lifes.&lt;/p&gt;
&lt;p&gt;After reading the CLR version 2 spec, and seeing covariance was not handled in Framework 2.0, I hoped it will be in 3.0. Then in 3.5. Try to make C# at least aware of what is specified in a delegate or interface, even if there is no language support to create it.&lt;/p&gt;
</description></item><item><title>WAW!!! What a serie of blog posts for "Covariance and Contravariance" - C# Future...</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx#9251505</link><pubDate>Wed, 24 Dec 2008 09:36:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9251505</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;
</description></item></channel></rss>