qualified type names for mixins

Jonathan Marler via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 15 20:26:28 PDT 2017


On Thursday, 15 June 2017 at 22:59:23 UTC, Adam D. Ruppe wrote:
> On Thursday, 15 June 2017 at 22:36:56 UTC, Jonathan Marler 
> wrote:
>> Doesn't work with eponymous templates, like std.traits.Flag.  
>> For example, make this code work:
>
> That uses `.stringof` which means it is useless for anything 
> except informational printing.
>
> Post the code you are actually doing (not writeln stuff, with 
> mixin) and I'll show you how to do it right.
>

Like I said the example I'm working with is my PR for the 
bitfields function:

https://github.com/dlang/phobos/pull/5441/files

The bitfields function takes an alias to type for each "bit 
field", and then returns a string that can be passed to mixin.  
As you can see, it uses T.stringof, so to get the std.traits.Flag 
enum to work I had to add some "hacky template voodoo".

wilzbach commented on a particular template on line 76:

private template TypeName(T)
{
     static if (__traits(compiles, TemplateOf!T))
     {
         enum TypeName = T.stringof ~ "!" ~ 
TemplateArgsOf!T.stringof[5..$];
     }
     else
     {
         enum TypeName = T.stringof;
     }
}

He said he was surprised a version of this wasn't already in 
phobos and I responded saying it doesn't exist AFAIK, likely 
because it's very hard or not possible with currently language 
features.  My hacky attempt will only work with basic template 
types and isn't near a solution that works in the general case. 
However I think it could be implemented with 
relativeQualifiedName!T (as shown in my first post of this 
thread) or possibly by modifying some things in the language to 
make fullyQualfiedName!T work in more cases.

If you have a better idea on how to implement the bitfields 
template that would be great.  So far it seems you feel that this 
isn't a problem because any code that runs in to this problem 
must be "wrong" in the first place.  Maybe you're right, I would 
love to see a better solution, but I haven't thought of one. 
Thanks.





More information about the Digitalmars-d mailing list