Discussion: Permit tuple element names in deconstruction only if they match with source (same as patterns) #3304
Replies: 3 comments
-
This would be a breaking change, and would also require code like this to be significantly more verbose: class Foo
{
void M(Player player, Target target)
{
var (playerX, playerY) = player.GetPoint();
var (targetX, targetY) = target.GetPoint();
// ...
}
} Perhaps it would be simpler for an Analyzer to warn on deconstructions that we can see are likely to be incorrect (e.g. swapped variable names)? |
Beta Was this translation helpful? Give feedback.
-
Currently any use of names in deconstruction produces an error (CS8187). The suggestion is to relax that and only error on mismatched names, just like what we have for patterns. |
Beta Was this translation helpful? Give feedback.
-
Oh, I think I understand now. So this would still be valid syntax (but a program error): var (count, sum) = e;
var (sum, count) = e; But this new syntax would be valid: (count: int count, sum: int sum) = (count: 3, sum: 9); And this new syntax would be invalid: (sum: int sum, count: int count) = (count: 3, sum: 9); ? |
Beta Was this translation helpful? Give feedback.
-
It is useful for self-documenting code and also to make sure we're capturing the right elements
If we're deconstructing with Deconstruct, names must match with parameter names.
Note: Patterns already behave as above for both tuples and Deconstruct.
Beta Was this translation helpful? Give feedback.
All reactions