Scriptlike v0.9.4 - Perl-like interpolated strings, full examples and more.

Sönke Ludwig via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Tue Sep 22 22:44:55 PDT 2015


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




More information about the Digitalmars-d-announce mailing list