Proposal: Enum flag operation shorthand #7199
-
I would like to propose a shorthand way of handling enum operations to keep the resulting code less wide and more concise. I normally am not one to step into this realm of syntactic sugar, but this one I feel warrants consideration. When performing
Not only is this just extremely redundant code to both read and process mentally, it also promotes the next person reading the code to skim it since it isn't very tidy. Not to mention (at least, in the above example) this promotes making attributes multi-line, which has it's own cons in of itself. Instead, this is what I propose:
Breaking it down it would be of the following components:
Original Proposal - kept for posterityThe operator I am proposing is `:` - The presence of this operator immediately following an Enum type is what would signify the beginning of the shorthand. Since this shorthand will always result in an object of the declared enum's type, there is no need for any kind of character to signify the end of the shorthand. Instead, the end should be whenever any other object reference would normally end. For example: Since it is possible currently to compile
If another local member outside of the parentheses has the same identifier as an enum value that is being referenced inside of the parentheses then an ambiguous reference error should be thrown. For example:
Please let me know your thoughts - I genuinely feel this would be a big improvement to the language and would make reading these operations MUCH easier and much more concise. EDIT - Syntax change from original proposal@CyrusNajmabadi Brought up that ternary operations would introduce ambiguity into the code - I am ammending this proposal to introduce a slightly different syntax that (as far as I can tell) should resolve that ambiguity:
My reasoning behind this is that it is currently possible to call an empty constructor off of an enum and have it return the default value for that enum (though it isn't common in practice). This would allow the enums members to be clearly defined as locally referenceable in just that single expression, while making it clear what the developer's intentions were with using this syntax (plus I would imagine it's a tad less difficult to implement in this way as a bonus haha). The use of the I have updated all the above examples to reflect the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
There's a proposal already championed which would cover not only enum members but also static members on other types: #2926 |
Beta Was this translation helpful? Give feedback.
-
NOte: this woudl be challenging in a few contexts. First, Dictionary-Literals (part of the collection literals spec) uses Second, |
Beta Was this translation helpful? Give feedback.
-
I also wanted to share this:
There is a major advantage to this for sure |
Beta Was this translation helpful? Give feedback.
So, under the target-typed name lookup, why not do this when you feel the enum name aids in readability?