hasStaticMember and enums

Steven Schveighoffer schveiguy at gmail.com
Mon Sep 21 23:33:17 UTC 2020

I was confused today when I was looking to see if a particular field 
name was a field of a struct and not a static member.

I thought __traits(hasMember, T, item) && !hasStaticMember!(T, item) 
would suffice. But then I found that this returns true for enums that 
are part of the type.

struct S
    enum e = "hello";

static assert(__traits(hasMember, S, "e"));
static assert(!hasStaticMember!(S, "e"));

The reason is because hasStaticMember ultimately uses:

alias sym = Alias!(__traits(getMember, U, member));
enum hasStaticMember = __traits(compiles, &sym);

which of course doesn't compile for an enum.

But I would actually consider an enum to be a static member. It's a 
member, but does not consume any part of the instance.

The docs for hasStaticMember are pretty slim, but it was added here: 

There is no mention of how this should play with enums. Would it be bad 
to add a check for enums in this? I was thinking of changing the 
__traits(compiles) line to:

__traits(compiles, (auto ref a) { } (sym));

or something like that.


I'm also not sure why std.meta.Alias is used instead of a normal alias 
(maybe that used to be an issue? I tried just a straight alias and it 
works at least in this case).


More information about the Digitalmars-d mailing list