Update statement #2943
Replies: 10 comments
-
I'm confused by the motivation section. Why not simplify it to: var obj = GetOrDefault() ?? new Person();
obj.Name = name;
obj.Age = age;
return obj; ? |
Beta Was this translation helpful? Give feedback.
-
When the obj has so many properties to update, sometimes we update same property twice, or miss updating a property due to switch and if clutters. |
Beta Was this translation helpful? Give feedback.
-
So your proposal includes desired intellisense behaviour that isn't defined in your proposal? |
Beta Was this translation helpful? Give feedback.
-
The restriction that no identifier may be duplicated among an updater list is defined in the proposal, but the desired intellisense behavior isn't. I think it's common sense and I apologize that I included it in my previous argument. I'm crossing that off. |
Beta Was this translation helpful? Give feedback.
-
Would have been nicer if we could just do something like this:
and then change the example made by @yaakov-h to the following:
or even your example to this:
So instead of having it as a prefix to make it an infix, I think it's slightly more useful. Not sure about the syntax but yeah, just a thought. |
Beta Was this translation helpful? Give feedback.
-
@eyalsk does your syntax mutate object's properties in place or does it create a new copy of the object like record's |
Beta Was this translation helpful? Give feedback.
-
@ronnygunawan it's a syntactic sugar to the following:
So it just a convenient way to set the properties for the instance in bulk, nothing more. |
Beta Was this translation helpful? Give feedback.
-
Good idea. The keyword obj = obj as {
Name = name,
Age = age
}; can be translated to: if (obj != null) {
obj.Name = name;
obj.Age = age;
} We can also try to cast obj first before mutating it: var student = obj as Student {
Name = name,
Age = age,
Grade = grade
}; which will be translated to: if (obj is Student student) {
student.Name = name;
student.Age = age;
student.Grade = grade;
} My only concern is whether keyword obj = obj apply {
...
};
var student = (obj as Student) apply {
...
};
var obj = (GetOrDefault() ?? new Person()) apply {
...
}; |
Beta Was this translation helpful? Give feedback.
-
@ronnygunawan Well, I dunno enough keywords are used in C# for different things so it's all about knowing what it does in a given context. |
Beta Was this translation helpful? Give feedback.
-
Sounds like a good use case for an analyzer that can detect duplicate reassignments. Having a dedicated syntax for mutating an instance in place via object initializer sounds a bit unnecessary. Having an expression form also feels really confusing as it would seem to create a new instance, not change the existing instance. IMO I'd like to see a way to apply initializers to expressions. This has come up before where people want to initialize a class returned by a factory. If the language supported that it should be very easy to add an identity factory that just returns the same instance that you pass to it. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm proposing a new syntax which allows mutating multiple fields or properties of an object in one statement.
The token
update
is a new keyword.Each identifier_with_setter and identifier_with_getter on the left of an updater must bind to an accessible instance field or property of the type of the primary_expression. There may be no duplicated name among these identifiers of a given updater_list.
This statement does not create a new copy of the object referenced by primary_expression.
An update_statement of the form
is translated to
An update_statement of the form
is translated to
The construct is supposed to be analogous to object initializer and record withing.
Motivation
Code analyzer suggests to simplify object initialization, making it easier for us to spot duplicate initializations. But we can't do the same to object updates. This syntax is intended to help us better organize code that updates an object.
is refactored by current analyzer to
and I think it would be really nice if we can also refactor it to
Beta Was this translation helpful? Give feedback.
All reactions