Difference between Scala's existential types and Java's wildcard by example?
Asked Answered
T

5

31

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

Trestlework answered 23/6, 2009 at 7:8 Comment(1)
imho, fotNelton seems to make some relevant additions omitted by the others, which would be interesting to know more about. It would be interesting to have the second half of the quote explained more clearly...Ardis
T
15

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

Trestlework answered 24/6, 2009 at 7:2 Comment(2)
A link to the whole conversation would be most usefullLeffler
There wasn't much more other than thisTrestlework
R
8

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

Russi answered 23/6, 2009 at 14:10 Comment(3)
I've added Martin Odersky's comment to my original question. Reading his response to Bill Venners, it feels as though existential types are not equivalent to wildcards. Certainly, I'm fairly sure I read something a while back essentially puffing that Scala not having wildcard types (as it originally didn't) was a positive thing.Trestlework
Someone has pointed out to me that it was probably puffing the fact that Scala does not have raw typesTrestlework
@oxbow_lakes, is List[_] considered a raw list in Scala?Pipage
M
8

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.

Modeling answered 2/11, 2012 at 13:38 Comment(0)
A
4

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.

Apparitor answered 23/7, 2012 at 17:28 Comment(1)
Java's wildcard also supports lower bound. public List<?> foo(List<? super Integer> lst) { return lst; }Telugu
M
1

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.

Marchellemarcher answered 15/6, 2015 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.