Disallow Array[Nothing], Array[Null], ... to avoid ClassCastException in some cases
See original GitHub issueSee also https://issues.scala-lang.org/browse/SI-7453
In scalac
:
scala> if (false) Array("qwe") else Array()
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
In dotty, thankfully:
scala> if (false) Array("qwe") else Array()
res1: Array[String] | Array[Nothing] = [Ljava.lang.Object;@1dd92fe2
But still in dotty:
scala> val x = if (false) Array("qwe") else Array()
x: Array[String] | Array[Nothing] = [Ljava.lang.Object;@5454d35e
scala> val y: Array[_ <: String] = x
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
We either need to disallow Array[Nothing]
, Array[Null]
, etc or we need to erase Array[_ <: X]
to Object
instead of [X;
.
It would also be much nicer if we could infer the empty array above to have type Array[String]
instead of Array[Nothing]
, I don’t know what prevents us from doing this currently.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
shouldn't this code produce a ClassCastException
No. That exception is thrown by the JVM when it detects incompatible types being cast at runtime. As others have noted, this is...
Read more >ClassCastException: Arrays$ArrayList cannot be cast to ...
It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not...
Read more >Handling the ClassCastException Runtime Exception in Java
The ClassCastException in Java happens when the JVM tries to cast an object to a class (or in some instances, an interface) and...
Read more >Empty arrays and collections should be returned instead of null
Returning null instead of an actual array, collection or map forces callers of the method to explicitly test for nullity, making them more...
Read more >Null safety - Kotlin
The only possible causes of an NPE in Kotlin are: ... To allow nulls, you can declare a variable as a nullable string...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
One quite radical idea would be to treat
Nothing
andNull
not as classes but as abstract types. That means we could not generate aClassTag
for them and therefore it would be impossible to create instances ofArray[Nothing] and
Array[Null]`The comment says that we might want to consider disallowing forming a
ClassTag[Null]
orClassTag[Nothing]
manually (they are not synthesized anymore). On the other hand, anyone who would do that in a sense does homegrown meta-programming, and it’s not clear to what extent we want to give soundness guarantees for such code. So I think it’s OK to close for now. If we feel like it, revisit the manual creation issue.