Scriptlike v0.9.4 - Perl-like interpolated strings, full examples and more.
Nick Sabalausky via Digitalmars-d-announce
digitalmars-d-announce at puremagic.com
Wed Sep 23 07:33:23 PDT 2015
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. :(
More information about the Digitalmars-d-announce
mailing list