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

前端 未结 5 1016
傲寒
傲寒 2020-12-08 10:20

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 wi

5条回答
  •  北海茫月
    2020-12-08 10:36

    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.

提交回复
热议问题