dotty-feature-requests icon indicating copy to clipboard operation
dotty-feature-requests copied to clipboard

Allow both guards and destructing in case

Open Adam-Vandervorst opened this issue 4 years ago • 1 comments

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") 
      ...
    }

Adam-Vandervorst avatar Jun 28 '20 07:06 Adam-Vandervorst

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?

TheElectronWill avatar Aug 08 '20 15:08 TheElectronWill