-
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: Object Destructuring #3298
Comments
This is effectively identical to the "Wither" building block proposed for Records. See #3137 under |
I believe this would introduce a couple of nasty failure modes for developers maintaining an existing system. When renaming an existing property, you might
In both these situations, there's no easy way to locate code that might be impacted by the rename affected because the property isn't explicitly referenced anywhere in the line of code. It's easy to postulate that extensive unit tests would catch the problem - but how many developers write unit tests for things the compiler does automatically? (e.g. I don't write unit tests to check that auto-properties are correctly implemented.) As outlined by @gafter in #3231
Implicitly copying all matching properties between two otherwise unrelated types would go against the principle currently driving language design. Having the compiler catch situations where things go wrong is extremely useful. |
It seems to be a common scenario to either do this:
OR
In both of these scenarios, it would be good to just be able to do:
OR
I would believe that non-existent properties should be ignored, so if input object has
string Else
and the target doesn't it should ignore. Obviously this could be consider bad if there was no warning saying properties are missed, it could introduce user bugs.I would say that this should be exhaustive, so tries to copy over all properties that exist on the input to the target. It should show compiler errors if the types are different.
You may be able to mix with throw expressions:
Although this would be a tough one, maybe.
I would expect this just to be syntactic sugar, so all the compiler does, is spit out the indivual lines, so resulting in a class that looked like the examples at the top.
Would be nicer than having to go through all the properties and getting them copied over.
The text was updated successfully, but these errors were encountered: