question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disallow Array[Nothing], Array[Null], ... to avoid ClassCastException in some cases

See original GitHub issue

See 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:closed
  • Created 7 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
oderskycommented, Feb 2, 2017

One quite radical idea would be to treat Nothing and Null not as classes but as abstract types. That means we could not generate a ClassTag for them and therefore it would be impossible to create instances of Array[Nothing] and Array[Null]`

0reactions
oderskycommented, Jan 26, 2018

The comment says that we might want to consider disallowing forming a ClassTag[Null] or ClassTag[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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found