Java 的使用站点差异与 C# 的声明站点差异相比如何? [英] How does Java's use-site variance compare to C#'s declaration site variance?

查看:21
本文介绍了Java 的使用站点差异与 C# 的声明站点差异相比如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,在 C# 中为泛型指定变化发生在类型声明级别:当您创建泛型类型时,您指定类型参数的变化.另一方面,在 Java 中,变异是在使用泛型的地方指定的:当您创建某个泛型类型的变量时,您指定其类型参数如何变化.

My understand is that specifying variance for generics in C# happens at the type declaration level: when you're creating your generic type, you specify the variance for the type arguments. In Java, on the other hand, variance is specified where a generic is used: when you create a variable of some generic type, you specify how its type arguments can vary.

每个选项的优缺点是什么?

What are the pros and cons to each option?

推荐答案

我只想回答声明站点和使用站点差异之间的差异,因为虽然 C# 和 Java 泛型在许多其他方面有所不同,但这些差异大多与方差正交.

I am just going to answer the differences between declaration-site and use-site variance, since, while C# and Java generics differ in many other ways, those differences are mostly orthogonal to variance.

首先,如果我没记错的话,使用站点差异比声明站点差异更强大(尽管以简洁为代价),或者至少 Java 的通配符是(实际上比使用站点差异更强大).这种增强的功能对于大量使用有状态构造的语言特别有用,例如 C# 和 Java(但 Scala 的作用要小得多,特别是因为它的标准列表是不可变的).考虑List(或IList).由于它具有添加 E 和获取 E 的方法,因此它相对于 E 是不变的,因此不能使用声明站点的变化.但是,对于使用站点差异,您只需说 List<+Number> 即可获得 ListList<-Number> 的协变子集得到List的逆变子集.在声明站点语言中,库的设计者必须为每个子集创建单独的接口(或类,如果您允许类的多重继承),并让 List 扩展这些接口.如果库设计者不这样做(请注意,C# 的 IEnumerable 只做了 IList 协变部分的一小部分),那么你就不走运了,你有诉诸于您必须在没有任何差异的语言中进行的相同麻烦.

First off, if I remember correctly use-site variance is strictly more powerful than declaration-site variance (although at the cost of concision), or at least Java's wildcards are (which are actually more powerful than use-site variance). This increased power is particularly useful for languages in which stateful constructs are used heavily, such as C# and Java (but Scala much less so, especially since its standard lists are immutable). Consider List<E> (or IList<E>). Since it has methods for both adding E's and getting E's, it is invariant with respect to E, and so declaration-site variance cannot be used. However, with use-site variance you can just say List<+Number> to get the covariant subset of List and List<-Number> to get the contravariant subset of List. In a declaration-site language the designer of the library would have to make separate interfaces (or classes if you allow multiple inheritance of classes) for each subset and have List extend those interfaces. If the library designer does not do this (note that C#'s IEnumerable only does a small subset of the covariant portion of IList), then you're out of luck and you have to resort to the same hassles you have to do in a language without any sort of variance.

这就是使用站点继承相对于声明站点继承的优势.声明站点继承相对于使用站点继承的优势对用户来说基本上是简洁的(假设设计者努力将每个类/接口分离为其协变和逆变部分).对于IEnumerableIterator 之类的东西,每次使用接口时不必指定协方差就好了.Java 使用冗长的语法使这一点特别烦人(除了双方差,Java 的解决方案基本上是理想的).

So that's the advantages of use-site inheritance over declaration-site inheritance. The advantage of declaration-site inheritance over use-site inheritance is basically concision for the user (provided the designer went through the effort of separating every class/interface into its covariant and contravariant portions). For something like IEnumerable or Iterator, it's nice not to have to specify covariance every single time you use the interface. Java made this especially annoying by using a lengthy syntax (except for bivariance for which Java's solution is basically ideal).

当然,这两种语言特性是可以共存的.对于自然协变或逆变的类型参数(例如在 IEnumerable/Iterator 中),请在声明中声明.对于自然不变的类型参数(例如在 (I)List 中),请在每次使用时声明您想要什么样的变化.只是不要为具有声明站点差异的参数指定使用站点差异,因为这只会使事情变得混乱.

Of course, these two language features can coexist. For type parameters that are naturally covariant or contravariant (such as in IEnumerable/Iterator), declare so in the declaration. For type parameters that are naturally invariant (such as in (I)List), declare what kind of variance you want each time you use it. Just don't specify a use-site variance for arguments with a declaration-site variance as that just makes things confusing.

我还没有涉及其他更详细的问题(例如通配符实际上比使用站点差异更强大),但我希望这能回答您对内容的问题.我承认我偏向于使用地点差异,但我试图描绘出在我与程序员和语言研究人员的讨论中出现的两者的主要优势.

There are other more detailed issues I haven't gone into (such as how wildcards are actually more powerful than use-site variance), but I hope this answers your question to your content. I'll admit I'm biased towards use-site variance, but I tried to portray the major advantages of both that have come up in my discussions with programmers and with language researchers.

这篇关于Java 的使用站点差异与 C# 的声明站点差异相比如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆