<?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 Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx</link><description>Suppose we were to implement generic interface and delegate variance in a hypothetical future version of C#. What, hypothetically, would the syntax look like? There are a bunch of options that we could hypothetically consider. Before I get into options</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 Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5767450</link><pubDate>Mon, 29 Oct 2007 19:27:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5767450</guid><dc:creator>Bryan Watts</dc:creator><description>&lt;p&gt;I see the logical issues involved with inference.&lt;/p&gt;
&lt;p&gt;The token is an explicit statement of intent which greatly reduces the scope of the problem - now you can tell the developer that their program is illegal.&lt;/p&gt;
&lt;p&gt;Also, I like the variance token as a suffix; it reads like a property of the type parameter instead of an operator.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5767544</link><pubDate>Mon, 29 Oct 2007 19:35:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5767544</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;I would prefer a suffix as well, though that does present a minor problem, as &amp;quot;interface IFoo&amp;lt;T-&amp;gt;&amp;quot; would be lexed as INTERFACE ID LEFTANGLE ID ARROW. &amp;nbsp;We'd have to do some minor surgery to the lexical grammar to make it work out.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5769768</link><pubDate>Mon, 29 Oct 2007 22:19:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5769768</guid><dc:creator>Richard</dc:creator><description>&lt;p&gt;How about a syntax like:&lt;/p&gt;
&lt;p&gt;interface I&amp;lt;:U, V:&amp;gt; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;U f(V);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Here, :T means covariant (a visual reminder of 'is derived from T'), and T: means contravariant (a visual reminder of 'derives from T'). It's not perfect, because derivation isn't the relationship we're capturing, but given:&lt;/p&gt;
&lt;p&gt;class A {}&lt;/p&gt;
&lt;p&gt;class B : A {}&lt;/p&gt;
&lt;p&gt;interface I&amp;lt;:U, V:&amp;gt; { U f(V); }&lt;/p&gt;
&lt;p&gt;It seems reasonable that this is OK:&lt;/p&gt;
&lt;p&gt;class C : I&amp;lt;A, B&amp;gt; { B f(A); }&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5772505</link><pubDate>Tue, 30 Oct 2007 02:03:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5772505</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Indeed, that is one of the syntaxes I will discuss next time, and the downside of it is exactly as you state. &amp;nbsp;I would rather not further overburden the notion of &amp;quot;derivation&amp;quot;. But it is a nice visual. (We were thinking of *:T, U:*, which is even more visually striking.)&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5773193</link><pubDate>Tue, 30 Oct 2007 03:02:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5773193</guid><dc:creator>Allan P</dc:creator><description>&lt;p&gt;I occasionally use variance at the IL level and am rather accustomed to the +- notation.&lt;/p&gt;
&lt;p&gt;I think it would do just as well in C#.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5777852</link><pubDate>Tue, 30 Oct 2007 11:22:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5777852</guid><dc:creator>Gabe</dc:creator><description>&lt;p&gt;Is it a bad idea to allow variance where deduceable and disallow it where it's ambiguous? Perhaps it means that changing an interface would make the new version incompatible with the old version.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5779292</link><pubDate>Tue, 30 Oct 2007 14:16:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5779292</guid><dc:creator>silky</dc:creator><description>&lt;p&gt;instead of an operator why not an attribute.&lt;/p&gt;
&lt;p&gt;[Variance(V=covariant,W=contravariant)]&lt;/p&gt;
&lt;p&gt;interface M&amp;lt;V, W&amp;gt; {&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;i've got to be honest in that i find this whole thing quite confusing. (compilable examples obviously help...) but i think that an attribute-based system will be more readable.&lt;/p&gt;
&lt;p&gt;it also seems a little more appropriate, imho.&lt;/p&gt;
&lt;p&gt;also, it obviously trivialises the job to integrate this into the 'hypothetical' language.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5779632</link><pubDate>Tue, 30 Oct 2007 14:53:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5779632</guid><dc:creator>Matt</dc:creator><description>&lt;p&gt;The attribute idea is an interesting one. I do prefer the more verbose syntax such things provide (You read and edit a class/interface many many more times than you write one) but appreciate it might make life harder for the compiler.&lt;/p&gt;
&lt;p&gt;It also brings up the question of how such variance is expressed within the reflection system...&lt;/p&gt;
&lt;p&gt;Making the attribute handle each iof the generic types could require some nasty hacks or etxtual code which could be messy. An alternate is to extend attribute locations to allow them to be placed on each generic type&lt;/p&gt;
&lt;p&gt;e.g. &lt;/p&gt;
&lt;p&gt;class Foo&amp;lt; [Variance(Variances.Covariant)] U, [Variance(Variances.Contravariant)] V&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;I don't like how needlessly verbose this is though. Perhaps&lt;/p&gt;
&lt;p&gt;class Foo&amp;lt; [Covariant] U, [Contravariant] V&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Which is two attributes but rather more readable (if a type is both add both).&lt;/p&gt;
&lt;p&gt;Of course what happens if you apply the attributes within a language which doesn't support it :(&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5780766</link><pubDate>Tue, 30 Oct 2007 16:48:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5780766</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;&amp;gt; Is it a bad idea to allow variance where deduceable and disallow it where it's ambiguous?&lt;/p&gt;
&lt;p&gt;If we disallow it when it is ambiguous then what do we do for the customer who wants variance, knows what variance they want, and has an ambiguous interface? &amp;nbsp;That will be a very common case since ambiguities are easy to make. We have to provide a syntax for that case so that the user can disambiguate explicitly. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;We would also want to provide a syntax for &amp;quot;no variance&amp;quot; that differed from &amp;quot;deduce variance&amp;quot;.&lt;/p&gt;
&lt;p&gt;Both facts speak to the need for variance to be always explicit, never inferred.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5780799</link><pubDate>Tue, 30 Oct 2007 16:51:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5780799</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;&amp;gt; how such variance is expressed within the reflection system&lt;/p&gt;
&lt;p&gt;That's easy to answer. See &lt;a rel="nofollow" target="_new" href="http://msdn2.microsoft.com/en-us/library/system.reflection.genericparameterattributes.aspx"&gt;http://msdn2.microsoft.com/en-us/library/system.reflection.genericparameterattributes.aspx&lt;/a&gt; for details.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5780829</link><pubDate>Tue, 30 Oct 2007 16:54:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5780829</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;&amp;gt; what happens if you apply the attributes within a language which doesn't support it &lt;/p&gt;
&lt;p&gt;Then that language probably ignores the attributes as it always has. &lt;/p&gt;
&lt;p&gt;Remember, variance as I am characterising it here is an extension to the assignability rules. A language implementer may choose to implement whatever assignability rules they want. C# 3.0 presently ignores the variance bits in Reflection; the feature I am proposing in this series would make some future C# start paying attention to those bits and allowing certain assignments which are presently illegal.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5794016</link><pubDate>Wed, 31 Oct 2007 09:50:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5794016</guid><dc:creator>brandf</dc:creator><description>&lt;p&gt;How about extending the existing generic constraint syntax?&lt;/p&gt;
&lt;p&gt;class Foo&amp;lt;T&amp;gt; where T: class, covariant, IBar&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>Community Convergence XXXV</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#5901053</link><pubDate>Mon, 05 Nov 2007 09:29:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5901053</guid><dc:creator>Charlie Calvert's Community Blog</dc:creator><description>&lt;p&gt;Welcome to the thirty-fifth edition of Community Convergence. We have an interesting and controversial&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#8513015</link><pubDate>Fri, 16 May 2008 16:12:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8513015</guid><dc:creator>Felipe</dc:creator><description>&lt;p&gt;What do you mean by cycles?&lt;/p&gt;
&lt;p&gt;Like here: &amp;quot;When we detect a cycle in base classes, we can throw...&amp;quot;&lt;/p&gt;
&lt;p&gt;I am brazillian and dont undestand very well...&lt;/p&gt;
&lt;p&gt;I can say that I'm your fan, and I think the articles of the blog incredible. &amp;nbsp;I would like your permission to translate them, we do not have anything like that in Portuguese, would be of great help to the community&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#8513060</link><pubDate>Fri, 16 May 2008 16:24:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8513060</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;By &amp;quot;cycles in base classes&amp;quot; I mean a situation such as:&lt;/p&gt;
&lt;p&gt;class A : B {} &amp;nbsp;&lt;/p&gt;
&lt;p&gt;class B : C {}&lt;/p&gt;
&lt;p&gt;class C : D {}&lt;/p&gt;
&lt;p&gt;class D : A {}&lt;/p&gt;
&lt;p&gt;That's not a legal class hierarchy. There is no way to eventually get to object as the base type. &lt;/p&gt;
&lt;p&gt;Similarly, it's not legal to have&lt;/p&gt;
&lt;p&gt;class C&amp;lt;T, U&amp;gt; &lt;/p&gt;
&lt;p&gt;where T : U&lt;/p&gt;
&lt;p&gt;where U : T&lt;/p&gt;
&lt;p&gt;And similarly with interface &amp;quot;inheritance&amp;quot;.&lt;/p&gt;
&lt;p&gt;Glad you like the blog!&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#8513755</link><pubDate>Fri, 16 May 2008 19:36:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8513755</guid><dc:creator>Felipe</dc:creator><description>&lt;p&gt;Thank you, but you did not say if I can translate and publish this series&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#8513813</link><pubDate>Fri, 16 May 2008 19:48:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8513813</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Fine by me, so long as you:&lt;/p&gt;
&lt;p&gt;* recognize that by doing so, it becomes impossible for me to update the content, fix errors, and so on, in your copy. &amp;nbsp;I do not want this to turn into more work for me.&lt;/p&gt;
&lt;p&gt;* do not quote me out of context.&lt;/p&gt;
&lt;p&gt;* take responsibility for any errors introduced by the translation process.&lt;/p&gt;
&lt;p&gt;* recognize that I am not the copyright holder. Microsoft Corporation owns the copyright to this blog. If in the unlikely event that Microsoft decides in the future that they don't want you to republish their content, please honour their requests.&lt;/p&gt;
</description></item><item><title>re: Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#8514211</link><pubDate>Fri, 16 May 2008 21:06:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8514211</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Also, something you might consider: rather than doing a direct translation of my work, why not write a blog of your own about these topics in Portuguese? &amp;nbsp;You can certainly use my blog as a resource; that's what it's for. &lt;/p&gt;
&lt;p&gt;I frequently blog about topics that I do not actually understand very well. (This may be obvious to some people!) I find that by deciding to write an article about a topic, I force myself to understand the topic well enough to explain to other people. &amp;nbsp;I learn a lot from writing this blog.&lt;/p&gt;
</description></item><item><title>C# 4.0: Covariance and Contravariance on generics…sort of…</title><link>http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#9024446</link><pubDate>Thu, 30 Oct 2008 16:46:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9024446</guid><dc:creator>LA.NET [EN]</dc:creator><description>&lt;p&gt;One of the things that we’ll have in C# 4.0 is covariance/contravariace working on generics…I mean, sort&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/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx#9251503</link><pubDate>Wed, 24 Dec 2008 09:36:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9251503</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>