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
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
They are supposed to be equivalent, as their main purpose is interacting with Java's wildcards.
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.
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.
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.
来源:https://stackoverflow.com/questions/1031042/difference-between-scalas-existential-types-and-javas-wildcard-by-example