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: Allow |= in object initializer syntax #2108
Comments
While I see the idea behind this, I feel this is untrue to the point of an object initializer, which should be used for initialization, not extending previously set values. It seems more proper to simply OR it in another line |
@johnkellyoxford: re. simplicity, what if you're passing the object as an argument? As it stands you have to declare a holder variable, OR the flags on another line, and only then pass it (forces a one-liner lambda into a multiline and braces). |
@emsaks yes it requires slightly more code, but initializer suggests initialization. Initializer isn't just a block of code that you can use to prevent having a multiline lambda. Maybe instead
|
|
@johnkellyoxford : I feel you're being too pedantic about the word 'initializer'; most fields in a class already have a default value too (usually |
so add another line? This seems kind of pointless. It doesn't really solve a major problem, all it would do is change some multiline lambdas to single line lambdas |
dup #109. |
@johnkellyoxford
What's the conceptual difference between adding a flag to the default flags and adding an item to the default items which is possible in today's syntax? var x = new Foo
{
Items = { addedItem },
ExistingObject = { ChangedProperty = 42 }
}; |
@jnm2 it does fit that for consistency, however I dislike that conceptually too honestly. I only use object initializer to initialize things that aren't initialized by the constructor, and then mutate things as I want post initializer |
Not all features need to solve a major problem |
😮 Every feature brings with it a cognitive load on every developer using C#. Every feature needs to be designed, implemented, tested and maintained - by a team that has limited resources (because every team has limited resources). This is why -100 points has practically reached meme status. If a feature isn't solving an actual problem, why bother? Note: I'm not expressing an opinion on the original post in this thread. |
@theunrepentantgeek I don't disagree, just poor wording on my part but what I meant is some features are added to the language and they are more a niche or a convenience than necessity and imo this feature falls into the niche category. |
@emsaks commented on Sat Dec 22 2018
It would be nice if you could use the
|=
operator within the object initializer syntax (i.e.new myObj { Attributes |= myObjAttribute.SomeFlag }
).This would enable adding a flag to existing flags set in the constructor.
[There's another proposal for extending the initializer syntax for event handlers here.]
@YairHalberstadt commented on Sun Dec 23 2018
I would suggest you move this request to dotnet/csharplang where language requests are taken
The text was updated successfully, but these errors were encountered: