On 09/23/2015 01:44 AM, Sönke Ludwig wrote:


An alternative idea would be to mix in a local "writeln" function, which
can then be used multiple times without syntax overhead:

mixin template interp()
{
     void iwriteln(string str)()
     {
         // pretend that we actually parse the string ;)
         write("This is ");
         write(somevar);
         writeln(".");
     }
}

void main()
{
     int somevar = 42;
     mixin interp;
     iwriteln!("This is ${somevar}.");
}


Hmm, interesting idea. I'd leave it as a string-returning function, rather than automatically printing, but a writeln convenience wrapper is a nice idea too.

The one problem I'm seeing with it though, is it wouldn't be able to see symbols declared between the "mixin interp;" and any later uses of it. Ie:

void main()
{
    int somevar = 42;
    mixin interp;
    iwriteln!("This is ${somevar}.");

    int another = 17;
    iwriteln!("This won't work, using ${another}.");
}

Seems like it would be too awkward and confusing to be worthwhile. :(

Reply via email to