-
Notifications
You must be signed in to change notification settings - Fork 207
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
Resolve typing treatment of throw/catch with NNBD #385
Comments
I don't see 1. and 2. as mutually exclusive. We could have: on Foo? catch (e) {
if (e is Foo) rethrow;
} |
@rrousselGit In 2 your code is a static error, in 1 it is not. That's the difference. In neither of them, can |
Where would be the static error? The I don't see any reason to have a compile error here. As long as re/throw matches the runtime error behavior, allowing or not While |
yes. "make it an error to have a potentially nullable type in the on clause" |
(The reasoning at the time was not that it would catch |
I have previosuly been in favor of allowing you to throw There is no strong reason to require the try {
T bailoutValue = ...;
if (bailoutValue != null) throw bailoutValue;
do something
} on T catch (bailoutValue) {
// Did a bailout
] would not have a way to type the catch. So my vote goes version 1. I'd probably be fine with disallowing potentially nullable types in the If you write no A I expect the analyzer to warn if you actually use a potentially nullable type in the The only question is whether we promote the |
I'd also vote for version 1. And I like the idea of promoting the |
I think approach 3 (allow throwing null, allow @lrhn mentions that it was confusing that the So the only remaining reason to go with approach 1 (or 2, but I also prefer 1 among those two) would be the one that @lrhn mentioned:
This does not fit very well with a construct which is essentially typeless anyway (expecting that we won't introduce Java-ish So we are weighing the anomaly of treating 'potentially nullable' types unsoundly in this single case versus the temptation to kill some more nulls. With that, I prefer having one the following 2 options:
Approach 3 consistently allows null for throwing, and the strict variant of approach 1 consistently prevents throwing null as well as testing for null. I think those options are the most meaningful ones for developers in the long run. |
I think I prefer 1b. It is consistent with the operand expression having a context type of I think that everywhere the specification requires a specific type ( |
SGTM. |
(For the record, it's now too late to change because all the async error handling APIs are expecting |
(Just curious, I think we're happy about the rule that it is a compile-time error to |
(We don't allow you to |
In comments on the NNBD feature spec the question came up of what errors and warnings to provide on throw catch. Currently, Dart makes it a runtime error to throw
null
. Apparently the reasoning given at the time was thatnull
would match anycatch
site, and users were unlikely to be prepared to receivenull
. With NNBD it's possible to distinguish whichcatch
sites are prepared to acceptnull
, so this reason goes away.There are three questions we should decide:
null
to be thrown?on clause
to specify a potentially nullable type?If we don't change the semantics to allow
null
to be thrown, then there is no point in having a nullable type in anon
clause. However, if you want to catch something of generic type you're out of luck:If we don't change the semantics of throwing
null
, then it seems somewhat unfriendly to allow the user to throw a nullable type, since it will always be an error if the value is actuallynull
. The same issue with generic types arises, but can be worked around by casting toObject
.The options that seem consistent to me are:
null
, make it an error to throw a potentially nullable type, and allow a potentially nullable type in theon
clause.null
, make it an error to throw a potentially nullable type, make it an error to have a potentially nullable type in theon
clause.Object?
and then rethrow based on anis
checknull
not an error, allow throwing potentially nullable types, allowing potentially nullable types in theon
clause.The current feature spec specified behavior 2. Should we change this?
cc @lrhn @munificent @eernstg @stereotype441
The text was updated successfully, but these errors were encountered: