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

Andrei Alexandrescu via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Thu Sep 24 17:00:53 PDT 2015


On 09/23/2015 01:44 AM, Sönke Ludwig wrote:
> 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}.");
> }

Yah, I think we need something like that in the stdlib. Also, we need 
writefln with compile-time format string (someone was working on it but 
I haven't heard about it in a while). -- Andrei



More information about the Digitalmars-d-announce mailing list