Skip to content

Star match with external capture fails #13844

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
Adam-Vandervorst opened this issue Oct 29, 2021 · 3 comments
Open

Star match with external capture fails #13844

Adam-Vandervorst opened this issue Oct 29, 2021 · 3 comments

Comments

@Adam-Vandervorst
Copy link

Compiler version

3.1.2-RC1-bin-20211027-435207d-NIGHTLY

Minimized code

  val qs = Seq(2, 3)
  Seq(1, 2, 3) match
    case Seq(b, `qs`: _*) => println("matched")
    case Seq(b, vs: _*) => println("missed")

Output

    case Seq(b, `qs`: _*) => println("matched")
                ^^^^
                Found:    (qs : Seq[Int])
                Required: Int

Expectation

Work the same as

  val q = 2
  Seq(1, 2) match
    case Seq(b, `q`) => println("matched")
    case Seq(b, p) => println("missed")
@bishabosha
Copy link
Member

bishabosha commented Nov 1, 2021

we get the same error if we use the syntactically correct version, suggesting that qs pattern bind label is not shadowing the qs in scope, but actually trying to match it? This is different to Scala 2.13 which will bind qs and not test for equality.

val qs = Seq(2, 3)
Seq(1, 2, 3) match
  case Seq(b, `qs` @ _*) => println("matched")
  case Seq(b, vs @ _*) => println("missed")

the following also fails, but syntactically this looks more like testing equality, so this behaviour seems ok to keep (it is also not allowed in 2.13):

val qs = Seq(2, 3)
Seq(1, 2, 3) match
  case Seq(b, `qs`*) => println("matched") // type mismatch of `qs`
  case Seq(b, vs @ _*) => println("missed")

this does not fail and binds qs to a new Seq:

val qs = Seq(2, 3)
Seq(1, 2, 3) match
  case Seq(b, qs*) => println("matched")
  case Seq(b, vs @ _*) => println("missed")

@Adam-Vandervorst
Copy link
Author

Hmm I used the syntax shown in the pattern matching docs. I agree that qs* looks like it should test for equality, as opposed to `qs` @ _* which matches anything and then tries to bind it to qs (which should warn on immutable variables?).

@bishabosha
Copy link
Member

bishabosha commented Nov 1, 2021

Hmm I used the syntax shown in the pattern matching docs

I see, I thought this syntax was meant to be removed in #11240, instead it was made a warning when you import language.future-migration, but as it was never in Scala 2.13 it seems strange to be there at all

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

2 participants