Scala type bounds versus Foo[_]

108 views Asked by At

I've a class Bar[T <: Foo]. What is the difference between type Bar[_ <: Foo] and Bar[_]? I don't see any semantical difference, since I can't write anything like new Bar[String]("asdf").

However, scalac does distinguish them. It looks like it forgets the bounds and consider Bar[_] to be a Bar[_ <: Any] rather than Bar[_ <: Foo].

Note that this works in Java as expected, unlike in Scala: https://gist.github.com/v6ak/7847548 . I wonder if there is an important semantical difference between Java and Scala I am not aware of. I know there are some differences, for example in generics with primitive types (especially when used together with arrays), but I don't know anything related to this issue.

To make things even stranger, I've disassembled the bytecode from the mentioned scala code (by reJ), disabled generics recognition and found that method foo returns always a Foo.

(I didn't use javap because I can't disable generics recognition there.)

1

There are 1 answers

0
ghik On BEST ANSWER

All of your examples compile in current milestone, 2.11.0-M7.

Relevant commits: this one and this one. It seems that this was fixed in 2.11.0-M3.