Search code examples
scalavariadic-functionsscala-3

`rest*` vs `rest @ _*` when pattern matching in Scala


val List(a, b, c, rest*) = ...

val List(a, b, c, rest @ _*) = ...

Both seem to work, but I've had situations where the former raises the error "bad simple pattern". Only occasionally though.

Why is this?


Solution

  • This is difference between Scala 2 and Scala 3 syntax. Just Scala 2 syntax is still working so far in Scala 3

    Scala 2:

    val List(a, b, c, rest @ _*) = ...
    

    https://scastie.scala-lang.org/SJbD2tTBQgiI4kF6Lun1rQ

    https://scastie.scala-lang.org/lCVAKK2WTlG99n6bBh4Tcw

    Scala 3:

    val List(a, b, c, rest*) = ...
    

    https://scastie.scala-lang.org/rV7UCBDaRzWyXLFNeRxxsg

    Docs:

    https://docs.scala-lang.org/scala3/reference/changed-features/vararg-splices.html

    https://dotty.epfl.ch/docs/reference/changed-features/vararg-splices.html