Allow Discards in Tuple Assignment within Switch Expressions to Preserve Existing Property Values #7633
Replies: 3 comments 8 replies
-
Does this mean that property assignment doesn't occur? Given that a property may have logic associated with setting the value, there's a very significant difference between assigning the current value of a property and eliding the assignment completely. With the original (current form): (Color, MainColor) = colorsArr.Length switch
{
1 => (colorsArr[0], MainColor),
2 => (colorsArr[0], colorsArr[1]),
_ => throw new Exception("SnackbarColors can only have 2 colors")
}; It's very clear that both With the proposed form: (Color, MainColor) = colorsArr.Length switch
{
1 => (colorsArr[0], _), // MainColor keeps its current or default value
2 => (colorsArr[0], colorsArr[1]),
_ => throw new Exception("SnackbarColors can only have 2 colors")
}; it's not clear whether the assignment happens. That is, if the first branch of the switch is taken, does If If With respect to your stated benefits:
The intent to keep the existing value is visible in the current form, when the user writes
Reducing code verbosity isn't in itself a benefit, unless widely used. Forms such as
I think the reverse is true. It makes the code more complicatec and would most likley introduce subtle bugs. |
Beta Was this translation helpful? Give feedback.
-
I think the concept of "Actually, just use the default value after all" is something that should be more general. For example, I've started to always use class Person
{
public required string FirstName { get; init; }
public required string LastName { get; init; }
public required Role Role { get; init; } = Role.Customer;
}
enum Role
{
Unknown = 0,
Customer = 1,
VIP = 2,
}
var person = new Person
{
FirstName = "Kenneth",
LastName = "Hoff",
Role = _,
} |
Beta Was this translation helpful? Give feedback.
-
I very occasionally come across the sort of scenario you describe. My approach to avoiding setting a property to itself is to return a bool too: (Color, mainColor. setMainColor) = colorsArr.Length switch
{
1 => (colorsArr[0], default, false),
2 => (colorsArr[0], colorsArr[1], true),
_ => throw new Exception("SnackbarColors can only have 2 colors")
};
if (setMainColor) MainColor = mainColor; Your solution is shorter, but for me these scenarios are real edge cases, so the feature isn't really justified. Plus I'm not convinced that it results in simpler code: it's just terser in my view. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation
When using tuple assignment with switch expressions, there are scenarios where we might want to update only some of the properties, leaving the others untouched. Currently, we have to explicitly specify the existing value for the property that we don't want to change. Allowing discards (_) in this context would simplify the code and make the intent clear.
Example
Current code without the feature:
Proposed code with the feature:
Benefits
Beta Was this translation helpful? Give feedback.
All reactions