How get struct value by member name string ?

bauss jacobbauss at gmail.com
Tue May 30 15:26:02 UTC 2023


On Tuesday, 30 May 2023 at 15:24:21 UTC, bauss wrote:
> On Tuesday, 30 May 2023 at 08:46:43 UTC, 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);
>>     }
>>
>> ...
>
> As simple as this:
> ```
> string getTMember(T t, string memberName)() {
>     return __traits(getMember, t, memberName);
> }
>
> ...
>
> writeln(getTMember!(t, "..."));
> ```

Noticed a mistake in my code ...

```
getTMember(T t, string memberName)()
```

Should be:

```
getTMember(T, string memberName)(T t)
```
And calling it should be like:

```
writeln(getTMember!("...")(t));
```


More information about the Digitalmars-d-learn mailing list