String interpolation

Paul Backus snarwin at gmail.com
Thu May 21 12:20:04 UTC 2020


On Thursday, 21 May 2020 at 06:57:28 UTC, mw wrote:
> On Thursday, 21 May 2020 at 01:13:27 UTC, Paul Backus wrote:
>> On Wednesday, 20 May 2020 at 23:41:15 UTC, mw wrote:
>>> Can we do string interpolation in D now?
>>
>> There's an implementation in the dub package "scriptlike":
>>
>> https://code.dlang.org/packages/scriptlike#string-interpolation
>
> Thank you! very nice:
>
> // Output: The number 21 doubled is 42!
> int num = 21;
> writeln( mixin(interp!"The number ${num} doubled is ${num * 
> 2}!") );
>
> Is there any way to make it shorter? e.g.
>
> writeln( s!"The number ${num} doubled is ${num * 2}!" );
>
> i.e how to write this 's'?

Unfortunately, it's not possible. Without `mixin`, there's no way 
to give the template access to local variables like `num`.


More information about the Digitalmars-d-learn mailing list