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

前端 未结 5 1028
傲寒
傲寒 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

    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.

提交回复
热议问题