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: Allow |= in object initializer syntax #2108

Open
gafter opened this issue Dec 27, 2018 · 12 comments
Open

Proposal: Allow |= in object initializer syntax #2108

gafter opened this issue Dec 27, 2018 · 12 comments

Comments

@gafter
Copy link
Member

gafter commented Dec 27, 2018

@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

@john-h-k
Copy link

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

@emsaks
Copy link

emsaks commented Dec 28, 2018

@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).

@john-h-k
Copy link

@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

  • Refactor the constructor to allow passing of additional flags for ORing
  • Use multiple lines, as initialization block shouldn't be used for mutating an object, it should be used for initializing it from nothing

@Thaina
Copy link

Thaina commented Dec 28, 2018

&= should also allowed too

@emsaks
Copy link

emsaks commented Dec 28, 2018

@johnkellyoxford : I feel you're being too pedantic about the word 'initializer'; most fields in a class already have a default value too (usually 0 or default(T)) and you change it to what you want. Flags are sort of an edge case that there may be a default flag set (say UnprocessedFlag) and you need to add another flag (i.e. IsResidentFile). The constructor is not always under your control and you don't always know what the default will be.

@john-h-k
Copy link

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

@iam3yal
Copy link
Contributor

iam3yal commented Dec 28, 2018

dup #109.

@jnm2
Copy link
Contributor

jnm2 commented Dec 29, 2018

@johnkellyoxford

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.

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 }
};

@john-h-k
Copy link

@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

@iam3yal
Copy link
Contributor

iam3yal commented Dec 29, 2018

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

Not all features need to solve a major problem or even a problem at all.

@theunrepentantgeek
Copy link

Not all features need to solve a major problem or even a problem at all.

😮

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.

@iam3yal
Copy link
Contributor

iam3yal commented Dec 30, 2018

@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.

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

No branches or pull requests

7 participants