英文:
What are possible difficulties in implementing return type covariance for interfaces?
问题
这个问题与这个问题高度相关。仅在C#的第9版本中才添加了对覆盖方法的返回类型协变性的支持,但接口仍然不受支持。我不关心如何克服这个问题(链接的问题已经有一个有效的答案),但我想知道在C#中支持返回类型协变性可能存在的潜在问题。
我的意思是,C++自C++98以来就支持协变返回类型。在C#中有什么不同,使得支持返回类型协变成为一个问题?在实现中可能存在哪些困难?从我的幼稚观点来看,这只是扩展编译检查的问题:不再要求精确的返回类型匹配,而是允许继承。这在类中已经做过了。我在这里错过了什么吗?
这个问题的目的不是比较哪种语言更好,而是理解问题背后的原因,这将有助于更好地理解该语言,从而更好地使用它。
英文:
The question is highly related to this one. The support of return types covariance for overriden methods was added to C# only in version 9, and still interfaces are not supported. I'm not interested in how to overcome this (the linked question has a valid answer already), but I wonder what are possible underlying problems with covariance return types support in C#.
I mean, C++ had covariance return type support for a very long time since C++98. What is different in C#, which makes the support of return type covariance an issue? What are the possible difficulties in the implementation? From my naive point of view, it's just the matter of extending a compilation check: instead of requiring an exact return type match, you allow inheritance. And this is already done for classes. What am I missing here?
The purpose of the question is not to compare which language is better, but to understand the rationale behind the issue, which will in turn help to understand the language better, which in turn will help to use the language better.
答案1
得分: 3
以下是您要翻译的内容:
"It's more complicated than it sounds, since it would lead to breaking changes."
Covariant Return Types 规范 也包含了支持接口协变性的草案。其中包括以下部分,我将直接引用(重点在于我的引用):
[接口协变性的拟议规范] 从技术上讲是一个破坏性变更,因为在当前版本中,下面的程序打印 "C1.M",但在拟议的修订版下会打印 "C2.M"。
using System; interface I1 { object M(); } class C1 : I1 { public object M() { return "C1.M"; } } class C2 : C1, I1 { public new string M() { return "C2.M"; } } class Program { static void Main() { I1 i = new C2(); Console.WriteLine(i.M()); } }
由于这个破坏性变更,我们可能会考虑不支持隐式实现的协变返回类型。
有关此功能的持续讨论可以在以下链接找到:
https://github.com/dotnet/csharplang/issues/49
在过去几年里没有太多进展,所以我猜目前社区可能不认为这是一个高优先级的扩展。
英文:
It's more complicated than it sounds, since it would lead to breaking changes.
The specification for Covariant Return Types also contains a draft for supporting covariance in interfaces. It includes the following section, which I will quote verbatim (emphasis mine):
> [The proposed specification for interface covariance] is technically a breaking change, as the program below prints "C1.M" today, but would print "C2.M" under the proposed revision.
>
>
> using System;
>
> interface I1 { object M(); }
> class C1 : I1 { public object M() { return "C1.M"; } }
> class C2 : C1, I1 { public new string M() { return "C2.M"; } }
> class Program
> {
> static void Main()
> {
> I1 i = new C2();
> Console.WriteLine(i.M());
> }
> }
>
>
> Due to this breaking change, we might consider not supporting covariant return types on implicit implementations.
The on-going discussion about this feature can be found here:
https://github.com/dotnet/csharplang/issues/49
Not much has happened in the last years, so I guess this is currently not considered to be a high-priority extension by the community.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论