为什么有人声称 Java 的泛型实现很糟糕? [英] Why do some claim that Java's implementation of generics is bad?
本文介绍了为什么有人声称 Java 的泛型实现很糟糕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我偶尔听说,对于泛型,Java 做的不好.(最近的参考,此处)
I've occasionally heard that with generics, Java didn't get it right. (nearest reference, here)
请原谅我的经验不足,但有什么能让他们变得更好?
Pardon my inexperience, but what would have made them better?
推荐答案
不好:
- 类型信息在编译时丢失,所以在执行时你无法判断它意味着"是什么类型
- 不能用于值类型(这是一个大问题 - 在 .NET 中,
List
确实由byte[]
支持,例如,并且不需要拳击) - 调用泛型方法的语法很烂 (IMO)
- 约束的语法可能会令人困惑
- 通配符通常令人困惑
- 由于上述原因的各种限制 - 铸造等
- Type information is lost at compile time, so at execution time you can't tell what type it's "meant" to be
- Can't be used for value types (this is a biggie - in .NET a
List<byte>
really is backed by abyte[]
for example, and no boxing is required) - Syntax for calling generic methods sucks (IMO)
- Syntax for constraints can get confusing
- Wildcarding is generally confusing
- Various restrictions due to the above - casting etc
好:
- 通配符允许在调用方指定协方差/逆变,这在许多情况下非常简洁
- 有总比没有好!
这篇关于为什么有人声称 Java 的泛型实现很糟糕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文