Proposal: Allow |= in object initializer syntax #8782
Replies: 12 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 |
Beta Was this translation helpful? Give feedback.
-
@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). |
Beta Was this translation helpful? Give feedback.
-
@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
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@johnkellyoxford : I feel you're being too pedantic about the word 'initializer'; most fields in a class already have a default value too (usually |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
dup #109. |
Beta Was this translation helpful? Give feedback.
-
@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 }
}; |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
Not all features need to solve a major problem |
Beta Was this translation helpful? Give feedback.
-
😮 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. |
Beta Was this translation helpful? Give feedback.
-
@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. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@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
Beta Was this translation helpful? Give feedback.
All reactions