Alex Rivera | Logout

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

Asked 2009-06-23T07:08:46.027
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

Edit
Report

1 Answer

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.

answered 2012-07-23T17:28:38.297

Your Answer