Skip to content

fix issue 9701 - allow UDAs to be attached to enum values#6161

Closed
ghost wants to merge 1 commit intomasterfrom
unknown repository
Closed

fix issue 9701 - allow UDAs to be attached to enum values#6161
ghost wants to merge 1 commit intomasterfrom
unknown repository

Conversation

@ghost
Copy link

@ghost ghost commented Sep 28, 2016

Only allows the UDA to be defined before the enum value, in such a way:

enum Enum
{
    @("value") value,
}

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:

enum Enum
{
    @("all"):
    hasAll,
    thisOneToo,

    @("extra"):
    hasAllAndExtra,
} 

@dlang-bot
Copy link
Contributor

Fix Bugzilla Description
9701 UDAs cannot be attached to enum values.

@schuetzm
Copy link
Contributor

schuetzm commented Oct 4, 2016

I don't think the : syntax needs to be supported. Elsewhere, it only works wherever a static if is allowed, which isn't the case in enum declarations.

@WalterBright
Copy link
Member

This is an enhancement, not a bug. As such, it needs a rationale for it.

@ghost
Copy link
Author

ghost commented Nov 5, 2016

@WalterBright

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 nothing

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

@CZDanol
Copy link

CZDanol commented Mar 7, 2017

Thumbs up, I also miss this feature.

@jpf91
Copy link
Contributor

jpf91 commented Mar 7, 2017

As such, it needs a rationale for it.

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

@JinShil
Copy link
Contributor

JinShil commented Nov 28, 2017

I've been informed by @WalterBright that this change will require a DIP.

@wilzbach
Copy link
Contributor

I've been informed by @WalterBright that this change will require a DIP.

@skl131313 submitted a DIP for this -> dlang/DIPs#105

@JinShil
Copy link
Contributor

JinShil commented Apr 12, 2018

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.

@JinShil
Copy link
Contributor

JinShil commented Jun 1, 2018

Revived at #8319

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants