[core.reflect] showcase fqn

Stefan Koch uplink.coder at googlemail.com
Thu Oct 7 23:33:07 UTC 2021


On Thursday, 7 October 2021 at 22:42:06 UTC, russhy wrote:
> Impressive results!
>
> I wasn't sold initially, but that was because i didn't know 
> what it was exactly, this totally is game changer
>
>  __traits is nice for simple stuff, but as soon as you try to 
> do more complex logic it starts to become unmanageable, and i 
> never remember the exact syntax, hopefully ``core.reflect`` 
> will solve that, and so far looks like it's already set to 
> replace __traits fully

Thanks. It is good to hear that.

I just  did a little work on the fqn utility.
This is the code which makes it print template instances should a 
template instance happen to be a parent.
```D

     TemplateInstance ti = 
cast(TemplateInstance)lastParentDecl.getParent();
     if (ti)
     {
         string argString;
         foreach(arg;ti.arguments)
         {
             if (auto il = cast(IntegerLiteral) arg)
             {
                 import std.conv : to;
                 argString ~= to!string(il.value);
                 argString ~= ", ";
             } else if (auto sl = cast(StringLiteral) arg)
             {
                 import std.conv : to;
                 argString ~= `"` ~ sl.value ~ `"`;
                 argString ~= ", ";
              }
         }
         result = ti.name ~ "!(" ~ argString[0 .. $-2]~ ")" ~ 
result[ti.name.length .. $];
         lastParentDecl = ti;
         d = cast(Declaration)ti.parent;
         if (d) goto Ldecl;
     }
```

So if the template argument is not a string or an integer this 
fqn function, (and it is an actual function)
would not be able to print it correctly.
However it is easy to add because it's just regular user-level 
code.


More information about the Digitalmars-d mailing list