-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Inconsistent behavior of String literal type between pattern matching and isInstanceOf #6996
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
The fact that pattern matching and isInstanceOf don't agree is definitely a bug, but
/cc @milessabin @sjrd, do you agree ? |
Yes, I believe that |
Hm, now that you mention it, I think we did talk about it already w/ @sjrd. My sole argument as to why this should not be the case is that all Something to consider - how should the following compile? val x: "a" = "a"
val y: Object = x
foo.isInstanceOf[x.type]
foo.isInstanceOf[y.type] |
That is a concern, indeed. But it is not limited to strings. You can also construct something with val x: 1 = 1
val y: Integer = x
foo.isInstanceOf[x.type]
foo.isInstanceOf[y.type] |
Ok, I just checked and apparently there's ample precedent for interesting behaviour when upcasting/boxing scala> val a: Any = 1
val a: Any = 1
scala> 1.isInstanceOf[a.type]
val res0: Boolean = true
scala> a.isInstanceOf[1]
val res1: Boolean = true
scala> (new Integer(1)).isInstanceOf[a.type]
val res2: Boolean = true
scala> val i: Integer = new Integer(1)
val i: Integer = 1
scala> val j: Integer = new Integer(1)
val j: Integer = 1
scala> i.isInstanceOf[a.type]
val res3: Boolean = true
scala> j.isInstanceOf[a.type]
val res4: Boolean = true
scala> i.isInstanceOf[j.type]
val res5: Boolean = false
scala> i eq j
val res6: Boolean = false
scala> i.isInstanceOf[1]
val res7: Boolean = true
scala> 1.isInstanceOf[i.type]
val res8: Boolean = false
scala> val o: Object = i
val o: Object = 1
scala> o.isInstanceOf[a.type]
val res9: Boolean = true
scala> a.isInstanceOf[o.type]
val res10: Boolean = false Note in particular that upcasting an |
upcasting ends up calling |
Maybe we need to reconsider the concept of literal singleton types, if we stop insisting that they're singletons, then I think that using |
Seems this needs more deliberations. |
Uh oh!
There was an error while loading. Please reload this page.
minimized code
Tried with dotr in Dotty compiler version
0.17.0-RC1
.expectation
new String("a")
should not match withcase _ : "a"
as it doesn't withisInstanceOf
.The text was updated successfully, but these errors were encountered: