Why are the bounds of type parameters ignored when using existential types in Scala?

后端 未结 2 526
予麋鹿
予麋鹿 2020-12-19 15:14

What I mean is this:

scala> class Bounded[T <: String](val t: T)
defined class Bounded

scala> val b: Bounded[_] = new Bounded(\"some string\")
b: B         


        
2条回答
  •  悲&欢浪女
    2020-12-19 15:34

    There was a lengthy discussion about this topic recently on the mailing list, Type Boundary "Stickyness" on Wildcards.

    It wasn't conclusive, other than to agree that existential types, such as Bounded[_] (a shorthand for Bounded[$1] forSome { type $1 }), don't lend themselves to intuition.

    @extempore did find one upside to the discussion :)

    On the plus side I'm finally reading the spec cover to cover. I had no idea the complete lyrics to "yellow submarine" were in the specification! Yet I have to admit, in context it was hard to see any other way that section could have been written.

提交回复
热议问题