core.reflect vs c++ reflection

russhy russhy at gmail.com
Mon Sep 27 22:51:01 UTC 2021


using traits:

```D
string enum_to_str(E)(E v) if (is(E == enum))
{
     final switch (v) with(E)
     {
         static foreach (m; __traits(allMembers, E))
         {
     case mixin(m):
             return m;
         }
     }
}
```

core.reflect is nicer than traits because you actually get to use 
actual D code

But the casting is kinda ugly, and it gives me java nightmares.. 
so the C++ actually feels much better




More information about the Digitalmars-d mailing list