Skip to content
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

Allow pattern case conditions in while statements. #3927

Open
lrhn opened this issue Jun 20, 2024 · 2 comments
Open

Allow pattern case conditions in while statements. #3927

lrhn opened this issue Jun 20, 2024 · 2 comments
Labels
feature Proposed language feature that solves one or more problems patterns Issues related to pattern matching.

Comments

@lrhn
Copy link
Member

lrhn commented Jun 20, 2024

Dart allows if (e case p when c) ....
It should also allow while (e case p when c) ....

The true branch is the loop body, which is where the bound values are available.

An example use could be finding the last element of a linked list:

  while (cursor.next case var next?) cursor = next;

Generally if and while are mostly symmetric, and the behavior here can be emulated by:

while (true) {
  if (cursor.next case var next?) cursor = next;
  else break;
}

(Even if Dart doesn't make (e case p when c) a general boolean expression, it can still safely and meaningfully be allowed in a few more cases, mainly [conditional expression][#2664] and while conditions. I doesn't make sense for do/while since the true branch isn't dominated by the condition.)

@lrhn lrhn added feature Proposed language feature that solves one or more problems patterns Issues related to pattern matching. labels Jun 20, 2024
@rrousselGit
Copy link

What about for?

for (...; something case ... ;  ...) {

}

If we can do it for while, I think it'd make sense here too.

@lrhn
Copy link
Member Author

lrhn commented Jun 20, 2024

The for should work too. It's a little crowded syntactically, and it's not completely clear whether any bound variables are in scope for the increment-expressions or not. Still, the check being true dominates the body and increments, so it should probably just work for those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems patterns Issues related to pattern matching.
Projects
None yet
Development

No branches or pull requests

2 participants