So, most people were wrong about this, and it also split opinion rather evenly... I suspect even that some of the people who were right were right for the wrong reasons! But the Scala compiler is actually being safe and correct, here. Explanation below...https://twitter.com/propensive/status/1284952925727580165 …
-
-
If something is a subtype of `A` and a subtype of `B`, then it is a subtype of `A with B`, the greatest lower-bound of `A` and `B`, or in this case, `List[_] with Seq[Int]`. It might be obvious how that could be simplified, but maybe not so obvious why.
Show this thread -
Type intersection (`with`) takes an *intersection* of the instances of each type, but defines a type which is the *union* of the properties of those types. Properties and instances of types are duals!
Show this thread -
So `List[_] with Seq[Int]` is the union of the properties of `List[_]` with the properties of `Seq[Int]`. Noting that `List[T] <: Seq[T]` and that both types are covariant, the compiler can deduce that `List[_] with Seq[Int]` simplifies to `List[Int]`.
Show this thread -
That means that the runtime test, combined with the static type, is sufficient for Scala to be certain that the scrutinee is not just a `List[_]` but a `List[Int]`. No warning is necessary. The covariance of `List` would allow us to safely match against, say, `List[AnyVal]` too.
Show this thread -
So, Scalac is spot on here, and not by luck, but by carefully following several nontrivial logical steps as a constraint solver. So, maybe we should have a bit more trust that it's doing the right thing, next time! ;)
Show this thread
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.