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

"while-case" and "do-while-case" #2536

Open
munificent opened this issue Sep 29, 2022 · 4 comments
Open

"while-case" and "do-while-case" #2536

munificent opened this issue Sep 29, 2022 · 4 comments
Labels
patterns Issues related to pattern matching.

Comments

@munificent
Copy link
Member

The patterns proposal adds a new "if-case" statement form for matching a single refutable pattern:

if (json case [int x, int y]) {
  print('Was coordinate array $x,$y');
} else {
  throw FormatException('Invalid JSON.');
}

Should we also support case inside while and do-while loops? I see no reason why not and it could sometimes be handy:

while (readNextLine() case [int x, int y]) {
  ...
}

do {
} while (readNextLine() case [int x, int y]);
@munificent munificent added request Requests to resolve a particular developer problem patterns Issues related to pattern matching. and removed request Requests to resolve a particular developer problem labels Sep 29, 2022
@munificent munificent added patterns-later and removed patterns Issues related to pattern matching. labels Nov 30, 2022
@munificent
Copy link
Member Author

This is useful, but I'm going to move it to later because I don't want to keep throwing new stuff at the poor beleaguered implementers.

@eernstg
Copy link
Member

eernstg commented Oct 25, 2024

With the do-while statement, though, it's quite surprising to have the declaration of x and y after the block where they are (presumably?) in scope. What's the value during the first iteration? ;-)

@munificent
Copy link
Member Author

OK, so maybe just while-case. :)

@lrhn
Copy link
Member

lrhn commented Nov 6, 2024

Agree. Recently wished I had while-case.

But just allowing case as a general expression, with scope extending to the dominated true branch if used as a condition, would give us while for free.
And allow it in do/while, but the variables can only be used in the test itself.

(That's why we need the full "fencepost" loop:

do { 
 ...
} while (e) {
 ...
} else {
  ...
} 

Where variables declared in the do part are also visible in the while and else parts (at least if there is no continue before their initialization), and pattern bound variables in the test condition are visible in the while part. A continue breaks the local block. Can I haz?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patterns Issues related to pattern matching.
Projects
None yet
Development

No branches or pull requests

3 participants