-
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
Promote associated variables when passing a tuple to a switch #3163
Comments
Currently the workaround is to shadow the variables: Object a;
Object b;
switch ((a, b)) {
case (final int a, final int b): // declare new variables
print('both are integers');
a.isEven; // works
} But that sounds a bit redundant. Outside of verbosity, this is quite error-prone too. It could be easily to incorrectly swap the names: switch ((a, b)) {
case (final int b, final int a): // oopsy
} |
This is variable/value aliasing. To be able to promote the We have something similar for the outer value of a switch: Object a = 1;
switch (a) {
case int(): print(a.isEven);
} This works because we know the value being switched on is the same value as the variable Object a = 1;
switch (a) {
case String(): print(a.substring(0)); // Valid!
case bool() when a = false: print("nope"); // Break link to variable.
case int(): print(a.isEven); // ERROR.
} Here the third case becomes an error, because while we're checking the same value in all the cases, If we can, somehow, keep the same kind of link between fields of a record (which is known to b immutable, that makes everything much simpler), we could allow checks against the record field to affect promotion of the aliased variable, as long as we believe it's still aliasing that variable. So, not completely impossible, but does require keeping more information around, tracking it, and invalidating it correctly. And also, should it only affect switch cases, or should it work in general? Object a = 1;
var pair = (a, 42);
if (pair.$1 is int) print(a.isEven); // Valid? That might get complicated fast. Or maybe it's trivial, what do I know 😉. (If we generally keep aliasing information for all the values, we could also update it on assignments. Object o1 = 1;
Object o2 = "String";
if (o1 is int) print(o1.isEven);
o1 = o2; // value of o1 now aliases both o1 and o2
if (o1 is String) { // Promotes both `o1` and `o2`, because we know it's the same value.
o1 = 42;
print(o2.substring(0));
} This could get even more complicated. But if we can track promotions through boolean variables, |
I believe "switch" is the main use-case. I'm not sure we'd want this for So: var a = 42;
var b= 21;
switch ((a, b)) {
...
} But not: var a = 42;
var b= 21;
final record = (a, b);
switch (record) {
...
} Basically, this is about making a switch case on multiple values at once. The idea is quite similar to #3160, and the pattern seems to be official (#2563 (comment)) |
Consider the following code:
It would be neat if the compiler detected that we were doing a pattern on a/b and promoted them accordingly.
This would be equivalent to:
The text was updated successfully, but these errors were encountered: