[Proposal] Flags Enum Initializer #1601
Replies: 7 comments
-
@Chi-TheCookie-Chi, I like seeing an issue for this problem because the longwinded flags enum If you're dealing with flags enums having fairly unique names, you can use using System;
using static System.AttributeTargets;
[AttributeUsage(Method | Property | Field | Constructor)]
class MyAttribute : Attribute { } This doesn't work so well if you're simultaneously using several flags enums in the same document that have some overlap in their flags' names. |
Beta Was this translation helpful? Give feedback.
-
That is really neat! However, I think the 'initializer' approach is more readable for the use context and less error prone. |
Beta Was this translation helpful? Give feedback.
-
#321 might solve this. |
Beta Was this translation helpful? Give feedback.
-
Not sure I like the initializer syntax. |
Beta Was this translation helpful? Give feedback.
-
If I read #321 correctly, we'd be able to re-express this (currently legal) code
as
Which I like very much - it doesn't introduce new syntax so much as just work the way people might expect. |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h Thanks for the link. I researched more on the topic and it seems there are many issues that are related to enum verbosity and I would like to add my input on these issues and related suggestions in an effort to shed some light and get new ideas on the subject.
I am against the second one since that is a naming conflict which should always result in an error and I don't see why enums should be an exception. Besides, wouldn't that make C# one of the few if not only programming language that has a defined behaviour for naming conflicts? |
Beta Was this translation helpful? Give feedback.
-
It would only be legal where it doesn't introduce an ambiguity, which would be relatively uncommon, but would not break any existing code. Where it would introduce an ambiguity it would be a compiler error and you could disambiguate by qualifying the enum. This syntax has more applications than the one that you've proposed, such as being used in direct assignments, comparisons and
Because you are bitwise ORing together the values of those enums? They represent bitflags afterall, not discrete values, and it's possible (and not terribly uncommon) for an enum member to span multiple other enum members. The fact that you are performing a mathematical operation over integral values in order to produce an integral value is an important one that really shouldn't be glossed over. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently we have the very handy object and collection initializers but we don't have anything for flags enums which tend to have long names for readability which soon turns into verbosity when specifying multiple flags.
An example of such verbosity is found in the .NET framework: AttributeUsageAttribute attribute which takes AttributeTargets enum flags which is defined for SerializableAttribute attribute as such:
[AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Delegate, Inherited = false)]
Possible fix:
[AttributeUsageAttribute(AttributeTargets { Class, Struct, Enum, Delegate } , Inherited = false)]
or use a pipe instead of commas (though this is inconsistent)
A possible issue to discuss is what to do when the enum doesn't have the FlagsAttribute attribute
Beta Was this translation helpful? Give feedback.
All reactions