Champion "pattern-based with
expressions"
#8876
Replies: 14 comments
-
Why is it tentatively planned for 7.2 if records themselves are in 8.0? |
Beta Was this translation helpful? Give feedback.
-
Records build on this, not vice versa. @MadsTorgersen are you working on a proposal for this? |
Beta Was this translation helpful? Give feedback.
-
I assume it's not only for records (where we need a new instance). For regular objects I think this should also work: e with { id = v }
->
(var _t = e; _t.id = v; _t) We have the same situation as |
Beta Was this translation helpful? Give feedback.
-
Not sure if I'm misunderstanding your example, but if |
Beta Was this translation helpful? Give feedback.
-
Yes, I think in the absence of a db.Persons.Find(id) with { Name = data.Name, ... }; or simply db.Persons.Find(id) with { data.Name, ... }; I think there is a need for null checked withers here which could be implemented via |
Beta Was this translation helpful? Give feedback.
-
@alrz this will change |
Beta Was this translation helpful? Give feedback.
-
@alrz, I'd see update blocks as unrelated to this. Having a with expression modify the original data would be really bad in my view. |
Beta Was this translation helpful? Give feedback.
-
I think like @DavidArno We should stay with the immutable state of the About the |
Beta Was this translation helpful? Give feedback.
-
I agree with @DavidArno |
Beta Was this translation helpful? Give feedback.
-
Actually the pattern-based class Person {
// ...
public Person With(string name = this.Name, int age = this.Age) { ... }
} So that something like (var _t = person; _t.With(name: "newName", _t.Age)) There is a lot more moving parts here but the simple initialization does not necessarily depend on it.
This is a known papercut that could simplify various applications and there's no alternatives for that either except for member-wise assignments.
The immutable state is not a property of
I don't know what it buys you since there's no guarantee with pattern-based |
Beta Was this translation helpful? Give feedback.
-
@alrz |
Beta Was this translation helpful? Give feedback.
-
But this will get taken care of by the great and powerful overload resolution like any other optional arguments, won't it? |
Beta Was this translation helpful? Give feedback.
-
I would very much like to see this added to a future language version to accomplish my example here regarding using the |
Beta Was this translation helpful? Give feedback.
-
#7752 - another case when this will be very convenient. (I don't force you to do this, it's all up to you) |
Beta Was this translation helpful? Give feedback.
-
In C# 9.0, we did
with
on records only, not some generalized pattern-basedwith
expression.See also
Design meetings
https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-09-26.md#construction-improvements
Beta Was this translation helpful? Give feedback.
All reactions