Scala 的存在类型和 Java 的通配符之间的区别?

发布于 2024-07-25 00:48:57 字数 650 浏览 9 评论 0原文

比 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绻影浮沉 2024-08-01 00:48:57

这是 Martin Odersky 在 Scala 用户邮件列表上的回答:

原始的 Java 通配符类型(如 ECOOP 论文中所述)
五十岚和维罗利)确实只是存在主义的简写
类型。 我听说并且在 FOOL '05 关于 Wild FJ 的论文中读到
通配符的最终版本与
存在主义类型。 我不知道到底在什么意义上(他们的
形式主义与古典存在主义类型相差太远,无法
能够查明差异),但也许仔细阅读《狂野》
FJ 的论文将对此提供一些线索。

所以看来 Scala 存在类型和 Java 通配符是等价的

This is Martin Odersky's answer on the Scala-users mailing list:

The original Java wildcard types (as described in the ECOOP paper by
Igarashi and Viroli) were indeed just shorthands for existential
types. I am told and I have read in the FOOL '05 paper on Wild FJ that
the final version of wildcards has some subtle differences with
existential types. I would not know exactly in what sense (their
formalism is too far removed from classical existential types to be
able to pinpoint the difference), but maybe a careful read of the Wild
FJ paper would shed some light on it.

So it does seem that Scala existential types and Java wildcards are kind-of equivalent

彩虹直至黑白 2024-08-01 00:48:57

它们应该是等效的,因为它们的主要目的是与 Java 的通配符交互。

They are supposed to be equivalent, as their main purpose is interacting with Java's wildcards.

热风软妹 2024-08-01 00:48:57

Martin Odersky 提供了更详细的答案(其余内容可以在此处找到):

Scala 需要存在类型来完成三件事。 首先
我们需要理解 Java 的通配符,并且
存在类型是我们对它们的理解。 第二个是我们
需要了解 Java 的原始类型,因为它们也是
仍然在图书馆里,未通用的类型。 如果你得到一个 Java raw
类型,例如 java.util.List 它是一个你不知道的列表
元素类型。 这也可以在 Scala 中用存在主义来表示
类型。 最后,我们需要存在类型来解释发生了什么
在 Scala 高层的 VM 中。 Scala 使用擦除模型
泛型,就像Java一样,所以我们看不到类型参数
当程序运行时就不再这样了。 我们必须进行擦除,因为我们需要
与 Java 进行互操作。 但是当我们进行反思时会发生什么
或者想表达虚拟机中发生了什么? 我们需要能够
表示 JVM 使用 Scala 中的类型执行的操作,以及
存在主义类型让我们这样做。 他们让你谈论类型
您不了解这些类型的某些方面。

A way more detailed answer by Martin Odersky (the rest can be found here):

Scala needs existential types for essentially three things. The first
is that we need to make some sense of Java's wildcards, and
existential types is the sense we make of them. The second is that we
need to make some sense of Java's raw types, because they are also
still in the libraries, the ungenerified types. If you get a Java raw
type, such as java.util.List it is a list where you don't know the
element type. That can also be represented in Scala by an existential
type. Finally, we need existential types as a way to explain what goes
on in the VM at the high level of Scala. Scala uses the erasure model
of generics, just like Java, so we don't see the type parameters
anymore when programs are run. We have to do erasure because we need
to interoperate with Java. But then what happens when we do reflection
or want to express what goes on the in the VM? We need to be able to
represent what the JVM does using the types we have in Scala, and
existential types let us do that. They let you talk about types where
you don't know certain aspects of those types.

场罚期间 2024-08-01 00:48:57

它们非常相似,但 Scala 的存在类型应该更强大。 例如,Scala 的存在类型可以是上界和下界,而 Java 的通配符只能是上界。

例如,在 Scala 中:

scala> def foo(x : List[_ >: Int]) = x
foo: (x: List[_ >: Int])List[Any]

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:

scala> def foo(x : List[_ >: Int]) = x
foo: (x: List[_ >: Int])List[Any]

the foo takes a list of parameter that has a lower bound of Int.

回首观望 2024-08-01 00:48:57

List[_] 表示法(正如其他答案指出的那样,它是 Java 的 List[?] 的更强大的模拟)是 Scala 中存在类型的更一般概念

The List[_] notation (which as other answers point out is a more powerful analog to Java's List[?]) is a degenerate case of the more general notion of an existential type in Scala.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文