www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Traits in a template enum

reply Some Guy <someguy mailinator.com> writes:
I have this enum to get the type of a member field in a struct: 
`enum typeOfMember(T, string member) = typeof(__traits(getMember, 
T, member));`

I'm having problems when I try to used it though. For example:

```D
writeln(typeOfMember!(T, member).stringof); // Doesn't work: 
Error: initializer must be an expression, not `int[]`

writeln(typeof(__traits(getMember, T, member)).stringof); // 
Expand the enum, it works.
```

What is the problem here? I'm using the LDC 1.27.1.
Oct 10 2021
next sibling parent Some Guy <someguy mailinator.com> writes:
It actually looks like I'm having problems wherever I try to pass 
that enum as a template parameter.
Oct 10 2021
prev sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Sunday, 10 October 2021 at 12:39:17 UTC, Some Guy wrote:
 I have this enum to get the type
enums hold values, not types. try alias instead
Oct 10 2021
parent reply Some Guy <someguy mailinator.com> writes:
On Sunday, 10 October 2021 at 12:48:49 UTC, Adam D Ruppe wrote:
 On Sunday, 10 October 2021 at 12:39:17 UTC, Some Guy wrote:
 I have this enum to get the type
enums hold values, not types. try alias instead
Thanks! `alias typeOfMember(T, string member) = typeof(__traits(getMember, T, member));` works. But I did not understand what you meant by "enums hold values, not types". Aren't types values at compile time?
Oct 10 2021
parent Mike Parker <aldacron gmail.com> writes:
On Sunday, 10 October 2021 at 12:56:30 UTC, Some Guy wrote:

 But I did not understand what you meant by "enums hold values, 
 not types". Aren't types values at compile time?
Types can be template arguments, if that's what you mean, but they aren't values.
Oct 10 2021