Interpolated strings

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 20 14:34:44 PDT 2017


On 4/18/17 4:50 AM, Walter Bright wrote:
> On 4/15/2017 4:51 PM, cym13 wrote:
>> Removing imports is a good point, the first concrete one to be
>> mentionned. I'm
>> not sure it matters that much though as I think those imports are
>> generic enough
>> that I believe they would be imported anyway, but it's a real point.
>
> It may not be necessary to have any dependencies on any import.
>
>     $"{a} times 3 is {a*3}"
>
> could be rewritten by the parser to:
>
>     "%s times 3 is %s", a, a * 3
>
> and that is that. (I.e. just an AST rewrite.) It would be quite a bit
> simpler than Jonas' proposed implementation.

Dmitry's solution is superior I think:

$"{a} times 3 is {a * 3}"

->

AliasSeq!(a, " times 3 is ", a * 3)

Would work fine with writeln. Would work fine with format. Wouldn't be a 
drop-in replacement for a string, as other languages like to do. But has 
huge potential.

I love Jonathan Marler's idea of using it for SQL inline grammar with 
prepared statements. Replacing something like:

sql("INSERT INTO table VALUES (?, ?, ?, ?, ?, ?, ?, ?)", a, b, c, d, e, 
f, g, h);

with:

sql($"INSERT row INTO table VALUES ({a}, {b}, {c}, {d}, {e}, {f}, {g}, 
{h})");

That is absolutely awesome.

What this does is match the parameter to where it belongs in the string, 
without all the extra quotes and commas.

And it does no GC activity, and doesn't require any specific formatting 
tokens!

-Steve


More information about the Digitalmars-d mailing list