-
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
Boolean pattern matches #3062
Comments
Yes, this is a cool idea. The obvious syntax would probably be var isNonEmptyList = this.node case ListExpression(
separator: ListSeparator.comma,
hasBrackets: false,
contents: [_, _, ...]
); The hard part is figuring out the precedence and determining when the pattern on the right is done and any surrounding expression begins. The expression and pattern grammars are both quite complex, so the current design works pretty hard to keep them clearly partitioned from each other so that the parser (and reader) can always tell when one ends and the next begins. If we limited the pattern after We would probably also have to restrict it by saying that any pattern used in this kind of expression can't bind variables because otherwise the scoping of those variables gets... weird. |
Binding variables is fine, but the if ((e1 case int x) && (e2 case int y)) { return x + y; } Promotion due to pattern matching would still work through the boolean variablen, like we do for other type checks today. I'm not too worried about delimiting the syntax, if we can just require parentheses in most contexts where there could be any doubt. |
As an additional note: if this is added, it would be nice to also have |
Edit: I just now noticed that Dart already has the I don't know about the feasibility of it in Dart, or if it has already been proposed, but a syntax similar to C#'s could be an option. It would basically consist of allowing patterns after if (this.node is ListExpression(separator: ListSeparator.comma, hasBrackets: false, contents: [_, _, ...]))
{
// ...
}
if (name is! String) {
// ...
} Binding the result to a new variable could simulate if (x is Foo(bar: Bar.optionA) foo && (foo.baz == 1 || foo.baz == 2)) {
// ...
} |
I've also noticed that not having these as expressions means I can write
but I can't write a expression where the pattern is not the primary boolean condition
Yes I know there's a |
@HENRDS, yes, we discussed using @amirburbea, I've run into this too, a number of times. In some cases, you can use a if (list case [..., MyType m]) { |
@munificent, that makes sense, thanks for explaining. |
As part of converting Dart Sass to Dart 3 style, I've noticed several cases where I wish I could use pattern matching just to express boolean conditions without any additional variable bindings. For example, I'd like to be able to write something like
instead of
...but there's not a great way to do that today. The best alternative is something much more awkward like
Edit: I did originally say "without any additional variable bindings", but I'm also running into cases where it would be useful to have a
when
clause that can access bound variables.The text was updated successfully, but these errors were encountered: