How get struct value by member name string ?

Steven Schveighoffer schveiguy at gmail.com
Tue May 30 15:43:12 UTC 2023


On 5/30/23 4:46 AM, John Xu wrote:
> How to put above enum as a function parameter? Following code wouldn't 
> work:
> 
>      string getTMember(T t, enum string memberName) {
>          return __traits(getMember, t, memberName);
>      }

compile time parameters come before runtime parameters:

```d
string getTMember(string memberName)(T t) {
    return __traits(getMember, t, memberName);
}

// used like
auto v = getTMember!"name"(t);
```

-Steve


More information about the Digitalmars-d-learn mailing list