Skip to content
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] Syntactic equivalent of object initialization for object properties re-assignment #11526

Closed
dg2k opened this issue May 24, 2016 · 6 comments

Comments

@dg2k
Copy link

dg2k commented May 24, 2016

Synopsis

Setting properties on object initialization makes the code readable with a single statement.

var customer = new Customer(){OrderID = order.ID, UserID = order.UserID, AddressID = order.AddressID, ... };

If I want to reuse the customer reference by reassigning properties, unless I am missing something, there is no compact form that is similar to initializing a new object. Each property setting requires a statement which means as many code lines.

Proposal

As a proposal, consider a keyword assign in place of new, not for creating an object, rather to assign property values. That is, assign is simply a syntactic equivalent of new initialization, but for assigning property values which otherwise requires more lines.

 customer = assign Customer(){OrderID = order2.ID, UserID = order2.UserID, AddressID = order2.AddressID, ... };

or

customer = assign {OrderID = order2.ID, UserID = order2.UserID, AddressID = order2.AddressID, ... };

Discussion

The above is just a convenience for re-assigning of property values to an existing object. The second version above even drops out the Type Customer as it should be obvious from the reference.

There are numerous usage scenarios where this can be convenient. To ready my reference for next use, for instance, I can assign default property values as:

customer = assign {OrderID = 0, UserID = 0, AddressID = 0, Name = "", IsOrderProssessed = false };

The assignment with object initialization syntactic equivalent makes the code more readable, particularly where a number of properties is large. There are of course caveats -- if the reference is not already initialized, the assignment will not work as expected. But then again, assign (or something equivalent) can act as new in this situation, in which case it acts to re-assign if the reference is already initialized, else create a new object. Makes sense?

Apologies in advance if an equivalent of this exists already but haven't come across yet.

@s-arash
Copy link

s-arash commented May 24, 2016

I propose this syntax:
With customer
.OrderId=0
.UserId=0
End With

@Unknown6656
Copy link

Unknown6656 commented May 25, 2016

Your proposal looks like a C# -equivalent of the VB-keyword with.
I am pretty sure, that this proposal has been made about 100 times before and I will reference the according issues when I found them ;)


EDIT:
This isssue is/may be a duplicate of:
#6553
#7322
#5172 (comment)
#5172 (comment)
#5676
#5445 (partly mentioned)
I can list more similar issues, when I return from University this evening :)

@dg2k
Copy link
Author

dg2k commented May 25, 2016

Thank you so much @Unknown6656 for taking the time to dig into similar past proposals. I've never assumed no-one thought of such a basic need in C# programming and the last sentence of my proposal confirms my very suspicion for the existence of similar ideas.

But at a different level this confirms an important point -- why are proposals widely supported like this one not taken up? Duplication of the same idea are like votes for the same thing. I wish the search system is a bit better, with flagging of similar ideas upon editing (like SO question posting), in which case I could have avoided this duplication.

@Unknown6656
Copy link

Unknown6656 commented May 25, 2016

@dg2k I know exactly, what you mean, I also have the need for the proposal you made ;)

Regarding your point about GitHub's search system: GitHub is primarily based around source control and team project control, less around issue search etc. (I also absolutely love how SO implemented the posting system)

Regarding your point about why C# does not (yet) have this feature implemented: The developers focus was probably primarily concentrated on performance and other language aspects....


At least, the isssue #5172 is currently marked as 3 - Working, so that is something, I guess....

@dg2k
Copy link
Author

dg2k commented May 26, 2016

@Unknown6656 Thanks again for the update and I'm now more in tune with this particular feature request.

I understand GitHub's particular strength vis-à-vis SO -- if there is such thing (or is there?) as the Oscar of online productivity tools (or whatever more suitable phrase you can attach here), GitHub should get it. Of course, in my opinion ;-)

@gafter gafter closed this as completed Apr 28, 2017
@gafter
Copy link
Member

gafter commented Apr 28, 2017

Issue moved to dotnet/csharplang #512 via ZenHub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants