-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Support type promotion with object patterns #52325
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
Comments
What you want to use is a variable pattern: void main() {
Object? foo = 123;
switch(foo) {
case final int foo:
print(foo / 10);
default:
print(foo);
}
} |
Another case for dart-lang/language#2911. And as @blaugold points out, it does promote when the pattern actually checks the type. |
Oops, I didn't realize I was matching the type void main() {
Object? foo = 123;
switch(foo) {
case int():
print(foo / 10);
default:
print(foo);
}
} |
It's a very well known foot-gun in the new switch syntax. Everybody does that. Often more than once. I personally blame type literals for the problem, not switches (dart-lang/language#2393 😉). We plan to have a lint reminding you to do something else when you write |
I'm wondering if using an object pattern could promote the type in the same way an if statement does. For example, this code promotes the variable
foo
:But in Dart 3, a switch statement with an object pattern doesn't promote
foo
:The text was updated successfully, but these errors were encountered: