-
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
try <pattern>
#3179
Comments
I should have come up with better examples, but I was trying to communicate how the various pieces operate at their maximum (not that everything here should be used all at once!) Some more down to earth examples: // Emptying a linked list
while (true) {
try var item? = list.first else break;
item.unlink();
}
// Delay rendering a ListTile in a Stateful Flutter Widget until all data has loaded.
try var title? = this.title else return buildLoadingWidget();
try var description? = this.description else return buildLoadingWidget();
return ListTile(title: Text(title), subtitle: Text(description)); The portion to provide symmetry within patterns itself could be skipped, but I wanted to present the idea of a refutable -> irrefutable escape hatch. |
There are a lot of interesting ideas here! I don't find automatically using Your examples around map patterns are a pain point I have noticed where you'd like to use a map destructuring in an irrefutable context and fail gracefully on absent keys. I think I like your proposal on #2496 better for that, though. This example: try var item? = list.first else break; Looks very similar to #2537 to me. Using |
Can you show an example of how this is useful? None are coming to mind. |
I can't remember the specific place where I encountered this,. I think it was selecting between two fields for a variable binding where one field was optional (but not null) and the other was not. |
Supports statement-level refutable variable bindings with optional else clause, which may exit.
try <refutable pat> = <expr> [else <statement>]?;
The pattern is always run to completion, binding as many variables as possible.
On failure, variable bindings that failed to match are bound to their default or null.
If the else clause exits, flow analysis determines variable bindings are non-null/default.
To make it symmetric, introduce patterns that behave like their statement counterparts:
try <refutable pat>
prefix: Turns a refutable pattern irrefutable. Is equivalent to rewriting variable bindings tovar <ident> else null
.<refutable pat> else <expr>
suffix: On fail, irrefutably match against a compile-time constant value. Issue a compile time error if the constant does not irrefutably match.[var | final | Type] <ident> else <expr>
suffix: A special case of the else suffix that relaxes typing. On the happy path the original or inferred type is used. On the sad path the type of the default value is used. If either path is taken, the resulting type isUP(happy ^^, sad q_q)
.❤ Behavior change wishlist ❤:
<pat> || <pat>
becomes irrefutable if the RHS is irrefutable. Not needed but would be neat.Syntax examples:
The text was updated successfully, but these errors were encountered: