How get struct value by member name string ?

Steven Schveighoffer schveiguy at gmail.com
Thu Jun 1 15:38:08 UTC 2023


On 5/31/23 12:08 AM, John Xu wrote:

> 
> When render vibe.d diet template,
> 
>      string[] allMembers = __traits(allMembers, t);

enum allMembers = __traits(allMembers, t);

>      res.render!("index.dt", t, allMembers)
> 
> if I don't want write memberName one by one in diet template:
> 
>      table
>          - foreach(memberName; allMembers)
>              tr
>                  td #{memberName}
>                  td #{getTMember!memberName(t)}
> 
> Problem: memberName is not known at compile time.

The problem is that you stored the member name list as a runtime 
variable, and that is not known at compile time. Try the enum.

Or really, just `foreach(memberName; __traits(allMembers, t))` right in 
the diet template.

-Steve


More information about the Digitalmars-d-learn mailing list