No mixin scope for string mixins. Is that an oversight?

Ali Çehreli acehreli at yahoo.com
Tue Oct 29 16:16:46 PDT 2013


Template mixins can take scope names to resolve name conflicts:

   http://dlang.org/template-mixin.html

import std.stdio;

template Templ()
{
     int i;
}

void main()
{
     mixin Templ!();
     mixin Templ!();

     writeln(i);
     /* Error: deneme.main.Templ!().i at deneme.d(149327)
      * conflicts with deneme.main.Templ!().i at
      * deneme.d(149327) */
}

The solution is to use mixin identifiers:

     mixin Templ!() A;
     mixin Templ!() B;

The code compiles but of course one must specify which 'i' to use:

     writeln(A.i);

The same feature does not exist for string mixins:

   http://dlang.org/mixin.html

Is there a reason for the omission? Is there an enhancement request for it?

However, the workaround is surprisingly trivial. First, the problem:

import std.stdio;

void main()
{
     mixin ("int i;");
     mixin ("int i;");

     writeln(i);
     /* Error: declaration deneme.main.i is already defined */
}

The solution:

import std.stdio;

template TemplateMixinize(string Str)
{
     mixin (Str);
}

void main()
{
     mixin TemplateMixinize!("int i;") A;
     mixin TemplateMixinize!("int i;") B;

     writeln(A.i);
}

Does TemplateMixinize :p already exist in Phobos?

Ali


More information about the Digitalmars-d mailing list