-
Notifications
You must be signed in to change notification settings - Fork 209
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 destructing could assign values to existing variables #4235
Comments
A reason not to do that is that it makes pattern evaluation have side effects even if the pattern didn't match. Currently patterns can only invoke The model for patterns is that you put a value in, and you get either "no match" or "match with these new variables" as result, but no side effects are intended, especially for the patterns that didn't match. |
Your example doesn't require patterns - it can be written as a 1-liner: var (myValue1, myValue2, myValue3) = test? (instance.myField, instance.otherField, instance.thirdField) : (0, 0, 0); |
Thanks @tatumizer, I'm aware. I just dislike the current syntax for that, hence this issue for allowing a different one 😁. |
FWIW, I prefer single-assignment (final) variables whenever possible. Simultaneous assignment is perfect for this. var (myValue1, myValue2, myValue3) = test? instance.{(myField, otherField, thirdField)} : (0, 0, 0); What not to like here? :-) |
Today we have patterns that allow us to deconstruct a variable:
This only creates new variables after the
:
. I'd like to request a way for us to assign this to an existing variable. This could only work when we have this outsideif
/for
lines, like:The
:=
is only a representation here, I'm not certain this is the best solution. This would allow us to avoid creating multiple lines here.The text was updated successfully, but these errors were encountered: