如何Java的使用现场方差比较C#的声明,网站差异? [英] How does Java's use-site variance compare to C#'s declaration site variance?

查看:113
本文介绍了如何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.

什么利弊每个选项?

推荐答案

我刚要回答声明站点之间的差异,并使用现场方差,因为虽然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中少得多,所以,特别是因为它的标准列表是不可变的)。考虑列表< E> (或的IList< E> )。因为它有两个添加E和获得E公司的方法,这是不变的相对于E,因此不能被用来报关现场的差异。然而,随着使用现场方差你可以说清单1 +号码> 获得列表的协子集列表与LT; -Number> 获得列表的逆变子集。在声明中,站点语言库的设计者将不得不作出独立的接口(或类,如果你允许类的多重继承),每个子集,并具有列表扩展这些接口。如果库设计者不这样做(注意,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.

所以,超过申报现场继承是使用现场继承的优势。声明站点继承过度使用现场继承的优点是基本上是为用户(前提是设计师通过每一个类/接口分离成它的协变和逆变部分的精力去)简洁。对于像的IEnumerable 的Iterator ,这是不错的不要有指定的协方差您使用的接口每一次。 Java的使用冗长的语法(除了bivariance为其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)名单),声明你想要什么样的变异每次使用它的时间。只要不指定一个声明站点方差参数的使用现场方差,只是使事情变得扑朔迷离。

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天全站免登陆