-
Notifications
You must be signed in to change notification settings - Fork 205
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
Statements inside if conditions #443
Comments
Moved this issue under languages from dart-lang/sdk#37462 |
The feature allows a variable to be declared in an expression. The scope of the variable is the code dominated by that expression (up to the nearest enclosing block end, presumably). Of the two formats, I think I prefer the "declaration as expression" over the "statement as part of expression". That is foo(var tmp = 42, tmp + 1, tmp + 2);
if (var success = test()) {
...
}
...
if (success) { moreSuccessStuff(); }
while ((var current = cursor.next) != null) {
...
cursor = current;
} and it can also be used to define syntactic sugar like If the syntax works out, I think it's a reasonable syntax for a Whether if((Point point = parseCursorPosition(chars)) != null) {
// Do something with point
} should make If we ever add an while ((Point p = nextPoint()).magnitude < 2) {
plot(point);
} else {
print("Point out of range: $p");
} |
See also the Python "assignment expression" PEP (https://www.python.org/dev/peps/pep-0572/). |
Possibly not exactly what you had in mind, but with patterns, you can now do: if(parseCursorPosition(chars) case var point when point != null) {
// Do something with point
} It will correctly scope |
@munificent I am almost certain that you wanted to showcase pattern's full capacity 😉, but for future readers, you can do this: if(parseCursorPosition(chars) case var point?) {
// Do something with point
} Also seeing @lrhn example, are there any obstacles preventing patterns from being used in loops like : while (cursor.next case var current?) {
// Do something with current
} Or the syntax is a bit shady/does not work for all cases? |
No, no problems with supporting patterns in while loops, we just didn't get time to design and implement it. See: #2536 |
Statements inside control statement conditions to store it, check it and also scope it inside the control statement block would be very useful.
or
This feature is very popular in golang world.
Right now, this is possible:
The only problem is this leaks the point outside its scope.
The text was updated successfully, but these errors were encountered: