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
asInstanceOf doesn't issue due erasure warning #1558
Comments
Imported From: https://issues.scala-lang.org/browse/SI-1558?orig=1 |
@paulp said: You get a ClassCastException in this example because you casted it to something it isn't. I think a more applicable example is: class A
class B extends A
object Up extends Application {
val l:HashSet[A]=new HashSet[A]
l+=new B
val l2=l.asInstanceOf[HashSet[B]]
for (x <- l2) println(x)
} This works fine, as expected, and would surely not benefit from a warning. |
@cunei said: |
@paulp said: scala> val strs = Array("abc", "def")
strs: Array[java.lang.String] = Array(abc, def)
scala> val arr = strs.asInstanceOf[Array[AnyRef]] // no warning
arr: Array[AnyRef] = Array(abc, def)
scala> strs(0) = new AnyRef
<console>:6: error: type mismatch;
found : java.lang.Object
required: java.lang.String
strs(0) = new AnyRef
^
scala> arr(0) = new AnyRef
java.lang.ArrayStoreException: java.lang.Object I'll defer to more sophisticated type system users than myself on this, but I have always been under the impression that asInstanceOf is how you say "I know what I'm doing, please Mr. Type System, just do it." |
@odersky said: |
@SethTisue said: If we don't want a warning here, then it seems to me that a spec change is needed in order not to require one. see also #1447 |
Consider this code:
In this example,
isInstanceOf
would warn that B is eliminated by erasure, but surprisinglyasInstanceOf
issues no warning at at all (even though extracting an element from the convertedHashSet
results, of course, in aClassCastException
).The text was updated successfully, but these errors were encountered: