C#泛型 - 无需设计下界? [英] C# generics - without lower bounds by design?

查看:117
本文介绍了C#泛型 - 无需设计下界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读在工作,在那里他感叹引入泛型的Java 5的编码人员与约书亚布洛赫接受记者采访时他不喜欢具体的实施很大程度上是因为方差支持-Java的通配符,使得它不必要的复杂。

I was reading an interview with Joshua Bloch in Coders at Work, where he lamented the introduction of generics in Java 5. He doesn't like the specific implementation largely because the variance support—Java's wildcards—makes it unnecessarily complex.

据我所知,C#3没有任何类似明确,界通配符,例如:你不能声明一个方法PriceBatch这需要资产的collecton或任何资产的子类(无效PriceBatch(收集和LT ;?扩大资产>资产)在Java中)。

As far as I know, C# 3 doesn't have anything like explicit, bounded wildcards, e.g. you can't declare a method PriceBatch that takes a collecton of Asset or any Asset subclass (void PriceBatch(Collection<? extends Asset> assets) in Java?).

有谁知道为什么通配符和范围还没有被添加到C#?进行这些功能的故意冷落,使语言更简单,或者是这个东西,他们只是还没有得到解决,以实现吗?

Does anyone know why wildcards and bounds haven't been added to C#? Were these features intentionally left out to make the language simpler, or is this something they just haven't gotten around to implement yet?

编辑:圣烟,从埃里克利珀自己的意见!读他和保罗的有见地的意见后,我认识到,至少有上界的支持,而且上面的​​例子可以翻译成C#为:

Holy smoke, comments from Eric Lippert himself! After reading his and Paul's insightful comments, I realize that at least upper bounds are supported and that the above example can be translated to C# as:

void PriceBatch<T>(ICollection<T> assets) where T : Asset

下界,在另一方面,显然不支持作为埃里克在他的第二个意见,例如说:有可能是没有办法直接把这种(有点做作)的Java代码到C#:

Lower bounds, on the other hand, are apparently not supported as Eric says in his second comment, e.g. there is probably no way to directly translate this (somewhat contrived) Java code to C#:

public class Asset {}
public class Derivative extends Asset {}
public class VanillaOption extends Derivative {}

public static <T extends Asset> void copyAssets(Collection<T> src, Collection<? super T> dst) {
    for(T asset : src) dst.add(asset);
}

Collection<VanillaOption> src = new ArrayList<VanillaOption>();
[...]
Collection<Derivative> dst = new ArrayList<Derivative>();
[...]
copyAssets(src, dst);



我是正确的?如果是这样的话,有没有为什么C#有上限而不是下限特别的理由?

Am I correct? If this is the case, is there a particular reason why C# has upper but not lower bounds?

推荐答案

一个复杂的问题。

首先让我们看看你的基本的问题,这是为什么非法在C#?

First let's consider your fundamental question, "why is this illegal in C#?"

class C<T> where T : Mammal {} // legal
class D<T> where Giraffe : T {} // illegal

这是一个泛型类型约束可以说T必须是能分到型哺乳动物的变量的任何引用类型,而不是T必须是任何引用类型,其中一个变量可能被分配一个长颈鹿。为什么会不同?

That is, a generic type constraint can say "T must be any reference type that could be assigned to a variable of type Mammal", but not "T must be any reference type, a variable of which could be assigned a Giraffe". Why the difference?

我不知道。那是我长的时间对C#队之前。琐碎的回答是因为CLR不支持,但设计的C#泛型球队是的原班人马的所设计的CLR泛型,所以真的没有太多的解释。

I don't know. That was long before my time on the C# team. The trivial answer is "because the CLR doesn't support it", but the team that designed C# generics was the same team that designed CLR generics, so that's really not much of an explanation.

我的猜测是简单地认为一如既往地予以支持功能必须设计,实施,测试,记录并运送给客户;没有人做过的那些事了此功能,因此它不是语言。我没有看到一个大的,令人信服的好处所提出的功能;没有引人注目的优点复杂的功能往往被削减这里周围。

My guess would be simply that as always, to be supported a feature has to be designed, implemented, tested, documented and shipped to customers; no one ever did any of those things for this feature, and therefore it is not in the language. I don't see a large, compelling benefit to the proposed feature; complicated features with no compelling benefits tend to be cut around here.

然而,这是一个猜测。下一次,我碰巧与谁仿制药工作的人聊天 - 他们住在英国,所以它不是像他们刚刚从我大厅,遗憾的是 - 我会问

However, that's a guess. Next time I happen to chat with the guys who worked on generics -- they live in England, so its not like they're just down the hall from me, unfortunately -- I'll ask.

至于你具体的例子,我认为保罗是正确的。你不需要下限的限制,使在C#中的工作。你可以说:

As for your specific example, I think Paul is correct. You do not need lower bound constraints to make that work in C#. You could say:

void Copy<T, U>(Collection<T> src, Collection<U> dst) where T : U 
{ 
    foreach(T item in src) dst.Add(item);
}



也就是说,把约束的T,而不是U.

That is, put the constraint on T, not on U.

这篇关于C#泛型 - 无需设计下界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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