UDAs on enum members
Walter Bright via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jul 17 13:51:42 PDT 2016
On 7/13/2016 4:57 AM, Tomer Filiba wrote:
> It would be really nice if I could put UDAs on enum members as well, e.g.,
>
> enum MyEnum {
> @("SOM") SomeMember,
> @("ANO") AnotherMemberWithAVeryLongName,
> }
Not a bad idea. It's been asked for before.
> And while we're on the subject, why can't enums have methods? At the risk of
> sounding as if I like Java (I don't :) ), it's a really nice language feature.
> Back to our example:
>
> enum MyEnum {
> @("SOM") SomeMember,
> @("ANO") AnotherMemberWithAVeryLongName;
>
> string dump() {
> ... // `this` is a value, not a ref here
> }
> static MyEnum load(string name) {
> ...
> }
> }
>
> Basically just allow a semicolon at the end of the members, after which methods
> could appear. Adding members or whatever else Java has is an overkill -- just
> use a struct for that. But instead of lots of dumpMyEnum(MyEnum
> e)/loadMyEnum(string s) pairs, you could write myMember.dump()/MyEnum.load(s)
struct MyEnum {
@("SOM") enum SomeMember = 0;
@("ANO") enum AnotherMemberWithAVeryLongName = 1;
string dump() {
... // `this` is a value, not a ref here
}
static MyEnum load(string name) {
...
}
}
Not as nice, but gets the job done. But adding a new aggregate type comes with a
lot of baggage. I'm not convinced the complexity is worth the benefit, which
seems minor.
More information about the Digitalmars-d
mailing list