dotty-feature-requests
dotty-feature-requests copied to clipboard
Allow both guards and destructing in case
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")
...
}
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?