-
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
Variables initialized in a try-block are awkward to use going forward #1884
Comments
I see where you're coming from, but honestly I don't think this is necessary. There are perfectly valid reasons to need to declare variables specifically in the int computeResult() {
// First, get a number from user input
int number; // <-- this is needed throughout the function
try {
String input = getUserInput(); // <-- this is not needed outside the try
number = int.parse(input);
} on FormatException catch(error, stack) {
int numCalls = stack.toString().split("\n").length; // <-- not needed outside the catch
print("The stack trace is $numCalls lines long");
return -1;
}
// Now do the real logic
int result = 0;
for (int i = 0; i < number; i++) {
result += i * 2;
}
return result;
} Not to mention, the analyzer is smart enough to realize when |
An expression- e1 catch (e) e2 (the grammar should work out since try e1 catch (e) e2 instead makes it even simpler, but might as well just require parentheses instead of the The big issue with try {
.... anything ...
} catch (e) {
// Can't assume anything here
}
// Or here. anything happening inside the That's different from other code structures because if something throws inside those, the rest will be skipped too. We can assume that if we get to the following code, the prior code didn't throw. A |
Something along the same lines has been proposed before. The comparison was that since int number = int.parse(userInput) !! -1; Personally, I don't love it. Why do that when the much more familiar alternative exists? int number = int.tryParse(userInput) ?? -1; I think it's simpler for functions that expect certain exceptions to happen often to just return null in that case and let the existing null-safety mechanics kick in. The question of whether variables have been declared or initialized could be solved by changing it to "is this variable null" and using null-aware operators where necessary. In more complex cases where a variety of errors can occur and need special treatment, the full try/catch should be used instead. |
Why not just an out-of-scope/function-scoped variable declaration like Javascript's |
Dart already allows you to scope a variable to its enclosing function, you just need to move the declaration outside the try/catch. |
I dunno how common this is, but I could also see it helping in my proclivity to include as much detail as possible when setting up exceptions:
That'd take four separate |
Apologies in advance if this has been discussed before; I did look around a bit but didn't find anything.
A common situation I encounter while programming is this:
var foo = functionThatMightThrow();
When this happens, there are generally two options:
- or -
The former awkwardly splits the declaration and assignment. Not a huge problem but not all that pretty either, with a whole block for a single line and an extra line for the declaration. The latter is alright if what comes after is just a few lines of code but quickly becomes hard to read beyond that.
It'd be nice to have some syntactic sugar to clean this up a bit, e.g.
Bit dodgy on the exact syntax there.
Or perhaps something similar to the negative if-variable syntax discussed in #1201, where you declare the variable inside the statement, follow with a contingency that exits the enclosing context, then the variable is in scope for the rest of the block.
Not dead-set on any particular solution here; maybe some other language has a better way of doing this cleanly. Would be interested in hearing others' thoughts on this though.
The text was updated successfully, but these errors were encountered: