fix issue 9701 - allow UDAs to be attached to enum values#6161
Conversation
|
|
I don't think the |
|
This is an enhancement, not a bug. As such, it needs a rationale for it. |
|
It seems more like a bug/overlooked feature than an enhancement. An UDA can be applied to virtually anything that has a name that can be declared. Even in instances where it does nothing, eg aliases. struct SomeStruct { }
@("some uda string") alias ThatStruct = SomeStruct; // valid code
writeln(__traits(getAttributes, ThatStruct)); // prints nothingUDAs can be declared for an enum that is only a single value, which means you can do the following below. enum Enum
{
A, B, C, D
}
@("uda of A") enum Enum_A = Enum.A;
@("uda of B") enum Enum_B = Enum.B;
@("uda of C") enum Enum_C = Enum.C;
@("uda of D") enum Enum_D = Enum.D;
writeln(__traits(getAttributes, Enum_A)); // prints "uda of A"The workaround isn't pretty, it shouldn't have to exist considering that a UDA can be attached to virtually everything else. Including anonymous enums and single value enums. It's only natural that multi value enums have UDAs as well. |
|
Thumbs up, I also miss this feature. |
@WalterBright rationale: It's required in some serialization/deserialization libraries. The main reason are reserved identifiers or naming conventions: Consider this json format: {
"type": "in" | "out"
}vibe.D json can serialize enums as strings IIRC. enum Type {@name("in") in_, name("out") out_}
struct MyJSON{Type type;}
auto json = parseJSON!MyJSON(jsonString);Basically the same reason than for attributes on fields. |
|
I've been informed by @WalterBright that this change will require a DIP. |
@skl131313 submitted a DIP for this -> dlang/DIPs#105 |
|
A DIP for this is no longer required. See https://forum.dlang.org/post/gcgtotrhuirhcvvbxrfm@forum.dlang.org However, it appears the original contributor is no longer on GitHub. This PR will require someone to adopt it and carry it across the finish line. |
|
Revived at #8319 |
Only allows the UDA to be defined before the enum value, in such a way:
Not sure if the following should be allowed but I don't see why not, though would require reworking a bit of code. Not currently implemented with this commit: