You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to be able to use the if-case pattern matching, that when the if body returns, I'll have the access to pattern variable outside of the if. This will make early returning with if-cases possible. Consider the below:
Albert221
changed the title
if-case negation pattern variable outside of if
if-case negation pattern variable outside of if (better early returns)
Feb 24, 2025
This sounds like it can be generalized to "case expressions".
If expr case pattern [when expr] was an expression (likely one that needs parentheses in every non-trivial context), and its bindings were available on the true branch, just like promotions would be, then you could just write:
if (!(e casevar v?)) {
return;
}
v.something();
You could possibly also write
if (o.hasBanana && (o.banana case pattern)) {
// ...
}
I want to be able to use the if-case pattern matching, that when the if body returns, I'll have the access to pattern variable outside of the if. This will make early returning with if-cases possible. Consider the below:
Versus this, using if-case with a negated pattern match:
The
case!
is an arbitrary syntax. I'd like any syntax for that.Currently, to utilize early returns we need this:
Java has such feature, you can do
if (!(animal instanceof final Cat cat))
- https://docs.oracle.com/en/java/javase/17/language/pattern-matching-instanceof.html#subhead1The text was updated successfully, but these errors were encountered: