从语言角度来说,逆变与代表相关的意义如何?
来自维基百科:
协变:从更宽(双) 变窄(浮动)。
逆变:从较窄(浮动)转换为较宽(双精度)。
在 .NET 中,委托具有协变性,因为它允许委托的指定返回类型的派生类型成为它所引用的方法的返回类型。
委托还具有逆变性,因为它允许委托的指定参数(参数)的派生类型成为传递到它所引用的方法中的参数类型。
有了这两个与委托相关的定义,它们不应该都是协方差的吗?在两种情况下,委托都期望“更宽”的类型,但得到的却是“更窄的类型”。
有关两者的示例,请参阅此处来自 MSDN。
那么,当与代表相关时,“逆变”这个词在语言上有何意义呢?
From Wikipedia:
covariant: converting from wider (double) to narrower (float).
contravariant: converting from narrower (float) to wider (double).
In .NET, a delegate has covariance because it allows for derived types of the delegate's specified return type to be the return type of a method that it holds a reference to.
As well a delegate has contravariance because it allows for derived types of the delegate's specified arguments (parameters) to be the argument type passed into the method that it holds a reference to.
With these two definitions as they pertain to delegates, shouldn't they both be covariance? In both cases, the delegate expects a "wider" type, but is given a "narrower type".
See here for an example of both from MSDN.
So how does the word contravariance make sense, linguistically, when pertaining to delegates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://blogs.msdn.com/b/ericlippert/archive/2009/11/30/what-s-the-difference- Between-covariance-and-assignment-compatibility.aspx
最后一段对赋值兼容性有一个简洁的总结。
//从语言上来说,这在参数与返回的意义上似乎是合乎逻辑的 - 所以 //向前或向后的方向是关于进入或离开函数。
http://blogs.msdn.com/b/ericlippert/archive/2009/11/30/what-s-the-difference-between-covariance-and-assignment-compatibility.aspx
Last paragraph has a concise summary of assignment compatibility.
//Linguistically, this seems to be logical in the sense of parameters versus return- so the direction //forward or backward is with regards to going in or coming out of the function.
我现在不同意 SeanVDH 的回答。他说:“从语言上来说,这在参数与返回的意义上似乎是合乎逻辑的——所以前进或后退的方向是关于进入或离开函数的”。
相反,我认为这是来自 此处:
协方差保留 赋值兼容性 和逆变将其反转。协方差是一种加宽转换,而逆变是一种缩小转换。
示例:
这一认识是阅读 Eric Lippert 的文章(Jon 中快速跟踪代表章节的协变和逆变部分的结果) Skeet 的书《C# In Depth》,以及上面引用的链接。
I now disagree with the answer from SeanVDH. He says "Linguistically, this seems to be logical in the sense of parameters versus return- so the direction forward or backward is with regards to going in or coming out of the function".
Instead I think this is the answer which comes from here:
Covariance preserves assignment compatibility and contravariance reverses it. Covariance is a widening conversion, and contravariance is a narrowing conversion.
Example:
This realization was the result of reading Eric Lippert's article, the Covariance and contravariance section of the Fast-tracked delegates chapter in Jon Skeet's book C# In Depth, as well as the link above where the quote was drawn from.