-
Notifications
You must be signed in to change notification settings - Fork 1.1k
aliasing gets less precise type than Scala #2198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Is this an issue for anything in practice? |
In Dotty, |
There's also a special case in subtyping: #1448, I wasn't sure if that special-casing was the right way to go, so feel free to propose a better solution if this one doesn't work in some cases. |
The problem I have is the exhaustivity of the following program: object Test {
val Nil = scala.Nil
val X = 5
def foo1b[T](l: List[T]) = l match {
case Nil => true
case x::xs => false
}
} The checker will report that scala.collection.immutable.Nil.type <:< scala.collection.immutable.Nil$(Test.Nil) = false The checker can special case, but given that now we also have |
Ah indeed, that seems like a good reason for changing the inference rule (and reverting #1448). |
The following code can serve as test case, which is accepted by Scala but rejected by Dotty: object Test {
val nil = scala.collection.immutable.Nil
def f(x: nil.type): Int = 3
f(scala.collection.immutable.Nil)
} |
If you want to try to fix it, it should probably be done in |
Thanks @smarter for the pointer, I'll play with it . |
After some thinking, I think this issue might not need to be fixed, as long as:
With the last assumption, the exhaustivity check works well without problem. The justification is that code that avoids widening is very rare, maybe 1/10000, it doesn't make sense to make a rarity a rule in the language. In contrast, it justifies for programmers to pay additional efforts for a rare usage, i.e. to annotate the expected type with |
It's not actually that rare, it's a common pattern to alias objects in a different package, e.g. https://github.com/scala/scala/blob/2.12.x/src/library/scala/Predef.scala#L150, https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/io/package.scala#L16, etc. |
Since module classes are a compiler-generated construct that's not directly visible to programmers, it seems better not to automatically widen a module singleton to its underlying class. Fixes scala#2198.
Fix #2198: Don't widen module singletons
In Scala,
Nil
gets the typecollection.immutable.Nil.type
, but in Dotty it getsscala.collection.immutable.Nil$
.The text was updated successfully, but these errors were encountered: