-
Notifications
You must be signed in to change notification settings - Fork 214
Allow finally
keyword on if
constructions
#1825
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
Comments
also same for loops, like in python for, while /* , do */ {
if (some) {
print('break');
break;
}
} finally {
print('done');
} /* while */ |
When comparing alternatives and workarounds, you could include the following variant (which avoids code duplication): bool doFinally = true;
if (myString.startsWith("+")) {
parsedNum.setSign(1);
} else if (myString.startsWith("-")) {
parsedNum.setSign(-1);
} else {
doFinally = false;
}
if (doFinally) {
myString = myString.substring(1);
} Similarly: var doFinally = false;
while (someCondition) { // or `for (...) {`.
doFinally = true;
... // Actual work done by this loop.
}
if (doFinally) ... OK, we repeat So the motivation for having |
What I'd do is: checkSign: {
if (myString.startsWith("+")) {
parsedNum.setSign(1);
} else if (myString.startsWith("-")) {
parsedNum.setSign(-1);
} else {
break checkSign;
}
myString = myString.substring(1);
} Using labeled breaks efficiently allows a lot of control flow patterns that would otherwise requires extra boolean state and double-checking the same thing. (About the loop exit block, I do want that! #171). |
Do you really want to add a whole language construct for the sake of 1 variable that will do the same? Please, do not turn Dart into JS or Python or Kotlin or something like this. Use switch-case. If you can't live without it, use wrappers and extensions: void main() {
// If with wrapper
var myString = '+123';
int parsedNum = 0;
doFinally(
() {
if (myString.startsWith("+")) {
parsedNum = 1;
} else if (myString.startsWith("-")) {
parsedNum = -1;
}
},
() => myString = myString.substring(1),
);
print('$myString : $parsedNum');
// Loop with extension
() {
while (true) {
if (true) {
print('break');
break;
}
}
}.doFinally(() => print('done'));
// Let
() {
return 'Let';
}.let((it) => print(it));
}
void doFinally(
final void Function() fn,
final void Function() doFinally,
) {
fn();
doFinally();
}
extension FunctionDoFinallyX on Function {
void doFinally(final void Function() fn) {
this();
fn();
}
}
extension FunctionLetX<T extends Object?> on T Function() {
R let<R extends Object?>(final R Function(T it) fn) => fn(this());
} |
If I saw that code without knowing anything about the feature, my assumption would be that the I'm not opposed to more powerful control flow constructs, but I don't feel this one carries its weight, and my intuition is that |
Just forgot that this existed. |
Normally, the
finally
keyword is used withtry
-catch
constructions and the code therein will run regardless of whether thetry
code throws or not.There have been a number of times I've wanted something similar for
if
constructions. For example (just an example!!!), when parsing a numeral string into a custom numeric type, you might want to check the sign, and if the sign is found, increment the parse index or alternatively shorten the string being parsed to not contain the sign:Without
finally
, you have to do one of the following things:These alternatives are fine, but I think it would be more concisely expressed with
finally
, and be less prone to error.The exact meaning of
finally
here would be: execute this code if any branches of the condition are taken; do not execute it if no branches of the condition are taken.The text was updated successfully, but these errors were encountered: