On Wednesday, 23 September 2015 at 14:33:23 UTC, Nick Sabalausky wrote:
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. :(

This is why I argued for alternative mixin syntax in D some ... years? ... ago.

It'd be really cool to have a writefln overload that did this:

int somevar = 42;
writefln#("This is ${somevar}");

writefln#("Plus two and you get ${somevar+1}");

Which would just be shorthand for

int somevar = 42;
mixin writefln!("This is ${somevar}");

mixin writefln!("Plus two and you get ${somevar+2}");


I feel like a bit of syntax sugar could go a long way ;)

Reply via email to