Allow with operator to be used with non-record/struct types #8944
Replies: 2 comments
-
See #8876 There are technical reasons for the limitation, namely that unless the type adheres strictly to the pattern that it will result in decapitation. The compiler ensures that records adhere to that pattern for you. public class Person {
public string Name { get; init; }
public virtual Person Clone() { ... }
}
public class Student : Person {
public string Class { get; init; }
// forgot to override Clone
}
Person person = new Student() { Name = "Foo", Class = "Underwater Basket Weaving" };
var cloned = person with { Name = "Bar" };
Debug.Assert(cloned is Student); // oops, not a Student! |
Beta Was this translation helpful? Give feedback.
0 replies
-
As Halo pointed out, this is a duplicate of #8876. Closing as such. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
If I have a struct or a record, I can do this:
However this doesn't work if
someOtherObject
is, say, a class type, instead of a record or struct type. This seems like an arbitrary restriction and it would be useful to be able to use thewith
operator with other types such as classes.Beta Was this translation helpful? Give feedback.
All reactions