Skip to content

Disallow new Array[Null,Nothing] or allow classTag[Null,Nothing]. #9586

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

Open
sjrd opened this issue Aug 19, 2020 · 0 comments
Open

Disallow new Array[Null,Nothing] or allow classTag[Null,Nothing]. #9586

sjrd opened this issue Aug 19, 2020 · 0 comments
Assignees

Comments

@sjrd
Copy link
Member

sjrd commented Aug 19, 2020

We need to either disallow new Array[Null](0) and new Array[Nothing](0), or allow the materialization of ClassTag[Null] and ClassTag[Nothing]. Otherwise, things are inconsistent.

For example, currently we have the following:

object Test {
  def main(args: Array[String]): Unit = {
    println(new Array[Null](0)) // ok
    println(new Array[Nothing](0)) // ok

    println(scala.reflect.classTag[Null]) // No ClassTag available for Null
    println(scala.reflect.classTag[Nothing]) // No ClassTag available for Nothing
  }
}

Either all 4 statements should succeed, or all of them should fail to compile.

For context, disallowing ClassTag[Null] and ClassTag[Nothing] came from #1730 based on the rationale of avoiding a specific kind of ClassKindException. The example from that issue can still be reproduced today using new Array:

object Test {
  def main(args: Array[String]): Unit = {
    val x = if (false) new Array[String](0) else new Array[Nothing](0)
    val y: Array[_ <: String] = x
    println(y)
  }
}

results in

Exception in thread "main" java.lang.ClassCastException: [Lscala.runtime.Nothing$; cannot be cast to [Ljava.lang.String;
        at Test$.main(hello.scala:3)
        at Test.main(hello.scala)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant