Scala 的存在类型和 Java 的通配符之间的区别?
比 Stack Overflow 问题更具体一点什么是存在类型?< /em>,Scala 的存在类型和 Java 的通配符之间有什么区别,最好有一些说明性的例子?
到目前为止我所看到的一切,它们似乎相当相似。
一些参考资料。 Martin Odersky 提到了它们; Google 的我的问题的热门搜索:
MO:最初的通配符设计......受到存在类型的启发。 事实上,原始论文有存在类型的编码。 但当最终的 Java 设计出来时,这种联系就有点丢失了
A bit more specific than Stack Overflow question What is an existential type?, what is the difference between Scala's existential types and Java's wildcard, prefereably with some illustrative example?
In everything I've seen so far, they seem to be pretty equivalent.
A few references. Martin Odersky mentions them; Google's top hit for my question:
MO: The original wildcard design ... was inspired by existential types. In fact the original paper had an encoding in existential types. But then when the actual final design came out in Java, this connection got lost a little bit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是 Martin Odersky 在 Scala 用户邮件列表上的回答:
所以看来 Scala 存在类型和 Java 通配符是等价的
This is Martin Odersky's answer on the Scala-users mailing list:
So it does seem that Scala existential types and Java wildcards are kind-of equivalent
它们应该是等效的,因为它们的主要目的是与 Java 的通配符交互。
They are supposed to be equivalent, as their main purpose is interacting with Java's wildcards.
Martin Odersky 提供了更详细的答案(其余内容可以在此处找到):
A way more detailed answer by Martin Odersky (the rest can be found here):
它们非常相似,但 Scala 的存在类型应该更强大。 例如,Scala 的存在类型可以是上界和下界,而 Java 的通配符只能是上界。
例如,在 Scala 中:
foo 采用下限为 Int 的参数列表。
They are very similar but Scala's existential type is supposed to be more powerful. For example, Scala's existential type can be both upper and lower bounded whereas Java's wildcard can only be upper bonded.
For example, in Scala:
the foo takes a list of parameter that has a lower bound of Int.
List[_]
表示法(正如其他答案指出的那样,它是 Java 的List[?]
的更强大的模拟)是 Scala 中存在类型的更一般概念。The
List[_]
notation (which as other answers point out is a more powerful analog to Java'sList[?]
) is a degenerate case of the more general notion of an existential type in Scala.