Difference between Scala's existential types a

2019-01-17 03:13发布

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

5条回答
2楼-- · 2019-01-17 03:43

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.

查看更多
劫难
3楼-- · 2019-01-17 03:46

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

查看更多
走好不送
4楼-- · 2019-01-17 03:51

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.

查看更多
该账号已被封号
5楼-- · 2019-01-17 03:57

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

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-17 03:58

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.

查看更多
登录 后发表回答