-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Modify a struct-type property directly, and allow intialization from existing instance #1347
Comments
Is using |
How can I use it with existing properties in framework? |
Value copy semantics is the distinguishing characteristic of structs. You're not updating a property of that struct, you are overwriting the entire thing. I think that glossing over this would encourage using structs in ways that are discouraged. |
You can't. But what framework properties return mutable value types that you want to modify often? |
The one that I recall, although it's not about structs is: Note that this matter affects third-party codes as well: COM, NuGet .... etc. |
Addition: Any method that returns a Point, a Size, or a Rectangle in framework face the same problem, and it is widely used. There are many similar structs like Padding and others. |
Withers (#162) could be extended to work not only with immutable types, but with struct properties as well: |
@MohammadHamdyGhanem Indeed, that has nothing to do with structs. It will happen with any property that returns a disconnected value. These situations should be sufficiently rare and since you'd need a separate syntax anyway I don't see why it's such a problem to explicitly have to reassign the property. Again, in the case with |
The only times I can recall having to deal with setting struct properties:
|
I agree. But it can be more compact like this: |
@MohammadHamdyGhanem Or |
With= does not imply that the object should keep its old values. |
@MohammadHamdyGhanem Yes, it does. If the word |
Why not use Besides that, just use a class.. |
Suggestion:
Use special symbol (such as <=) to modify struct-type properties directly.
Example:
Suppose we have this struct:
and this property:
public Person User { get; set; }
We can't directly use:
User.Points += 1;
It will not compile.
So we have to do this:
I suggest to use this syntax instead to do the same job as the previous code:
User <= Points += 1;
Another approach:
it will be easier if the Type initializer can initialize the new instance by the values of another instance:
User = User {Points += 1};
In this syntax we allow two new things:
1- use an existing instance instead of a "new Person ()".
2- use the += inside the {} because ID have a previous value and can be modified.
The last syntax can be re-written as:
User += {Points += 1};
The text was updated successfully, but these errors were encountered: