The worst Phobos template (in binderoo)

David Nadlinger via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 29 09:02:53 PDT 2016


On Thursday, 29 September 2016 at 13:58:44 UTC, Eugene Wissner 
wrote:
> Any chance to get this one working:
>
> import std.typecons;
>
> enum Stuff
> {
> 	asdf,
> }
>
> void main()
> {
> 	BitFlags!Stuff a;
> 	mixin(__traits(fullyQualifiedName, typeof(a)) ~ " c;");
> }

This wouldn't be a correct use of the feature anyway, since it 
runs into all sorts of fundamental issues with imports/scoping, 
aliases and templates. Using .stringof/fullyQualifiedName to 
generate a reference to a type or symbol in a string mixin is a 
mistake, period. Unfortunately, the misconception that they *are* 
code generation tools is fairly wide-spread, to the point where 
dlang.org contains a similarly misleading comment. [1]

Just emit the typeof expression into the mixin string, e.g. 
`mixin("typeof(a) c;");`. It should be fairly easy to convince 
yourself that a similar rewrite is always possible, even if it is 
sometimes less obvious (in some situations, you might need to 
change some code to, say, pass on a template parameter `T` all 
the way to the point of the mixin() call instead of `T.stringof`).

  — David


[1] https://github.com/dlang/dlang.org/pull/380


More information about the Digitalmars-d mailing list