Closed flomebul closed 3 years ago
scala> (1: Any).isInstanceOf[Array[?]]
val res1: Boolean = true
yes, that's a regression since Scala 2; it should return false. good catch!
the others are not bugs. isInstanceOf
should never raise an exception, and asInstanceOf
is a promise to the compiler that you know what you're doing. that asInstanceOf
bypasses compile-time error checking (though it does insert a runtime cast to the erased type) is exactly why it exists
note that autoboxing isn't the culprit here, and Any
isn't the culprit either, nor is Integer
:
scala> Integer.valueOf(1).isInstanceOf[Array[?]]
val res4: Boolean = true
scala> (new AnyRef).isInstanceOf[Array[?]]
val res5: Boolean = true
The erasure of Array[?]
is Object
in Scala 2 and 3, so I guess Scala 2 does something special for arrays.
Guillaume's PR: #12103
Compiler version
scala-3.0.0-RC2
Minimized code
Expectation
1.asInstanceOf[Array[?]]
should raise an exception(1: Any).isInstanceOf[Array[?]]
should raise an exception or return false(1: Any).isInstanceOf[Array[?]]
should raise an exception