Am 22.09.2015 um 22:18 schrieb Nick Sabalausky:
=====================
String Interpolation:
=====================
https://github.com/Abscissa/scriptlike#string-interpolation

AFAICT, a string mixin is necessary to accomplish this in D, but
otherwise it works much like other languages:

--------------------------------------------
// Output: The number 21 doubled is 42!
int num = 21;
writeln(
     mixin(interp!"The number ${num} doubled is ${num * 2}!")
);
--------------------------------------------

The interpolated sections are handled via std.conv.text(), so they
accept any type.

Bikeshedding requested! I'm not 100% sold on the name "interp" for this
long-term. Suggestions welcome.


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}.");
}


Reply via email to