Skip to content

simplified vararg example error #11419

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

Closed
kpbochenek opened this issue Feb 15, 2021 · 2 comments · Fixed by #11423
Closed

simplified vararg example error #11419

kpbochenek opened this issue Feb 15, 2021 · 2 comments · Fixed by #11423
Milestone

Comments

@kpbochenek
Copy link
Contributor

Compiler version

3.0.0-M4-bin-20210214-80befc3-NIGHTLY

Minimized code

(from docs on unified varargs syntax)

    val arr = Array(1, 2, 3)
    val lst = List(0, arr*)                  // vararg splice argument
    lst match {
      case List(0, 1, xs*) => println(xs)   // binds xs to Seq(2, 3)
      case List(1, _*) =>                   // wildcard pattern
    }

Output (click arrow to expand)

[error] (run-main-0) java.lang.ClassCastException: [I cannot be cast to scala.collection.immutable.Seq
[error] java.lang.ClassCastException: [I cannot be cast to scala.collection.immutable.Seq
[error] 	at Main$.main(Main.scala:8)
[error] 	at Main.main(Main.scala)
[error] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] 	at java.lang.reflect.Method.invoke(Method.java:498)

It is simple main with code above. Also tried in dotty REPL built from master - same exception.

@tgodzik
Copy link
Contributor

tgodzik commented Feb 15, 2021

val lst = List(0, arr*) just adds the array as a last element, so probably something we should fix before the release.

@smarter
Copy link
Member

smarter commented Feb 15, 2021

object Test {
  def main(args: Array[String]): Unit = {
    val arr: Array[String] = Array("foo")
    val lst: List[String] = List("x", arr: _*)  
    println(lst)
  }
}

Scala 2:

arri.scala:4: error: no `: _*' annotation allowed here
(such annotations are only allowed in arguments to *-parameters)
    val lst: List[String] = List("x", arr: _*)
                                         ^
1 error

Scala 3:

4 |    val lst: List[String] = List("x", arr*)
  |                                      ^^^
  |                                      Found:    (arr : Array[String])
  |                                      Required: String

I thought this was fixed by #9963, but somehow that's not enough here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants