Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Allow both guards and destructing in case #128

Open
Adam-Vandervorst opened this issue Jun 28, 2020 · 1 comment
Open

Allow both guards and destructing in case #128

Adam-Vandervorst opened this issue Jun 28, 2020 · 1 comment

Comments

@Adam-Vandervorst
Copy link

Adam-Vandervorst commented Jun 28, 2020

Description

When matching, you have to choose to use a pattern binder with the @ notation or to destruct.
A lot of expressivity could be gained by allowing both, especially with #127

Current

    Foo(1, Bar(true), K) match {
      case Foo(1, b, k) if manual_check(b, k) => println(s"p is ${b.p}, k is $k") 
      // OR
      case f @ FooCheck() if f.x == 1 => println(s"p is ${f.bar_field.p}, k is ${f.k}") 
      ...
    }

Proposed

    Foo(1, Bar(true)) match {
      case Foo(1, Bar(p), k) @ Guard() => println(s"p is $p, k is $k") 
      ...
    }
@TheElectronWill
Copy link

TheElectronWill commented Aug 8, 2020

The x @ p syntax is the "pattern binder" (cf. the specification ), whereas the "guard" is the if condition.

You can already do

case f @ Foo​(​1​, b, k) => ...

With your own unapply function it seems to me that you could apply checks and extract values all at once. Is there a situation where this doesn't give enough flexibility?

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

No branches or pull requests

2 participants