Interpolated strings

Martin Tschierschke via Digitalmars-d digitalmars-d at puremagic.com
Mon Apr 17 12:12:37 PDT 2017


On Sunday, 16 April 2017 at 16:10:15 UTC, Nick Sabalausky 
(Abscissa) wrote:
> On 04/15/2017 04:35 PM, crimaniak wrote:
>> On Saturday, 15 April 2017 at 20:04:13 UTC, Jonas Drewsen 
>> wrote:
>>> The compiler will basically lower the $"..." string to a 
>>> mixin that
>>> concatenates
>>> the expression parts of the (inside the {}) and the plain 
>>> text parts.
>> It's easy implementable as a library (see
>> https://github.com/Abscissa/scriptlike#string-interpolation) 
>> so it does
>> not seem like a good idea to modify the language, only to 
>> change
>> interp!"" to $"".
>
> Yea, and note, I'm still open to the idea of better names than 
> "interp". I'm still not entirely happy with that name. I'm even 
> half-tempted to use "_".
>
> The only one problem I've found with doing it in library 
> though: Far as I could tell, it seems to require the caller 
> uses string mixins, which makes actually using it a little 
> uglier and more verbose than I would like.

I was trying to get it shorter:
// From
// Output: The number 21 doubled is 42!
int num = 21;
writeln( mixin(interp!"The number ${num} doubled is ${num * 2}!") 
);

defining a new method exho! (derived from echo + mixin...:-)

   auto exho(string x)(){
      return mixin("writeln("~interp!x~")");}

You can just write:

    exho!"The number ${num} doubled is ${num * 2}!"

This now looks more like "normal" scripting than

    writeln( mixin(interp!"The number ${num} doubled is ${num * 
2}!") );

> Maybe I'm overlooking something obvious, but I haven't been 
> able to find a way to change it to either a template mixin or 
> even just a plain template without sacrificing to whole point 
> of interpolated strings: specifying the arguments 100% inline.
>
> What I think would be ideal is a language enhancement to allow 
> "interp" to do its job without the extra syntactical noise. 
> That would not only give us good interpolates strings, but 
> would likely have other applications as well.

I am not against the idea making string interpolations part of 
the language, in vibe.d diet templates the extrapolation is 
marked with #{var}, the ruby style, I like this too.
At the beginning I thought this is already part of d.

Regards mt.



More information about the Digitalmars-d mailing list