-
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
Consider a shorthand for boolean fields #3114
Comments
Grammatically it should work. All other object pattern field entries require a It only works in refutable patterns, not assignment or declaration patterns, so we don't have to worry about someone just forgetting a colon in The boolean field match would only work for object patterns. The only other real option is map patterns, and That brings us back to type-safety. Is it OK to do If it's really just a shorthand for At least the meaning of the pattern With #2563, there might be more ambiguity. A |
I'm surprised I haven't made/seen this issue before. Big +1 I think it's one of the large pain points with the current pattern syntax. It's unnatural to do bool checks with It makes the syntax odd compared to the rest of Dart
If it's a I'd base myself on what works with an if (list.first) // works with List<bool> but not the rest |
Although I recon the problem may exist, I think the suggestion is actually very counter-intuitive. |
I've found myself fairly frequently needing to do pattern matches where I check boolean fields of the object I'm matching against. However, it's substantially more verbose to do that using the pattern matching syntax than a standard boolean expression one might use in an
if
/else
:object.isState
becomesType(isState: true)
. Some of this will be mitigated by #2563, but I suggest also allowingType(isState)
as a shorthand forType(isState: true)
. I think special-casing booleans makes sense given that pattern matching is an intrinsically boolean context.The text was updated successfully, but these errors were encountered: