-
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
[Pattern-matching] Allow :name syntax in switch statements #3092
Comments
The reason that syntax doesn't work is that: const foo = 42;
switch (o) {
case Bar(x: foo): ...
} means to check the We can't use the existing binding of the variable to determine that it should be a constant match, because you are allowed to introduce a new variable shadowing it. So you have to write |
But that's not I would expect: |
I found that I can omit parameter name and use var/final and it will do: @rrousselGit if I used |
At least an analyser error about "constant pattern" is confusing here. |
ACK, so the request is only about Currently the So, the request is to allow an plain identifier in this pattern position to be treated as if it was prefixed by The But it's not impossible. How a raw identifier is treated depends on the context and the kind of pattern. This proposal would put the pattern of an unnamed class C {
final int a;
C(this.a);
}
const a = 42;
void main() {
var c = C(42);
switch (c) {
case C(a: a && var b): print(b);
}
} I find it unlikely that we'll make this change, but it's not technically impossible. |
I personally think that this would be a significant user experience improvement. The cost of having to type that For comparison, here's how Freezed dealt with pattern matching before: AsyncValue<int> obj;
obj.when(
data: (value) => print('Hello $value'),
error: (error, stackTrace) => print('Error $error'),
loading: () => print('loading'),
) Here's the same thing implemented with Dart 3's pattern matching: AsyncValue<int> obj;
switch (obj) {
case AsyncValue(:final value?): print('Hello $value');
case AsyncValue(:final error?): print('Error $error');
case _: print('loading');
) There are a bunch of unnecessary types/keywords which get in the way. |
Even with #2563, I don't think the switch (obj) {
case AsyncData(:value): print('Hello $value');
case AsyncError(:error): print('Error $error');
case _: print('loading');
} and then the extra switch (obj) {
case AsyncData(:var value): print('Hello $value');
case AsyncError(:var error): print('Error $error');
case _: print('loading');
} I personally think it helps readability by showing that a variable is introduced at that point. I'm sure it can work either way. I'd probably require that if you can omit the |
My example doesn't use different types. Instead, it relies on null checks (although I forgot to type the The behavior isn't quite identical, but that's the most common case
I'm fine with that. I think that's the logical thing to do. Meaning my previous sample could become: AsyncValue<int> obj;
switch (obj) {
case .(:value?): print('Hello $value');
case .(:error?): print('Error $error');
case _: print('loading');
) |
Ack, the Then my requirement of not allowing more complicated patterns than |
It has one for stackTrace too, I just didn't use it here. They are rarely rendered. I think supporting I'd expect |
There's a shorthand syntax in object matching
I tried to use it in switch statements and expected it will work.
My expectation:
Data()
will be destructured and itsname
parameter will be binded to a corresponding newname
variable automatically.but it fails with following errors:
It works only if I use an explicit syntax
But it looks excessive.
It also ain't working in switch expressions:
Proposal:
Allow using the
:name
syntax in object matching in switch statements and switch expressionsThe text was updated successfully, but these errors were encountered: