Interpolated strings

Jonas Drewsen via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 19 08:07:55 PDT 2017


On Wednesday, 19 April 2017 at 14:02:43 UTC, Stefan Koch wrote:
> On Wednesday, 19 April 2017 at 12:10:33 UTC, Jonas Drewsen 
> wrote:
>> On Wednesday, 19 April 2017 at 12:03:47 UTC, Stefan Koch wrote:
>>> On Wednesday, 19 April 2017 at 11:59:51 UTC, Jonas Drewsen 
>>> wrote:
>>>
>>>> What about supporting an optional prefix inside the {} like:
>>>>
>>>> int year = 2017;
>>>> format($"The date is {%04d year}");
>>>>
>>>> so if there is a % immediately following the { then the 
>>>> chars until next whitespace is format specifier. You can of 
>>>> course leave out the format specifier and it will default to 
>>>> %s.
>>> I really don't see how string interpolation is better then
>>> ` "The date is " ~ format("%04d", year)); `
>>
>> As mentioned before it is only because it is such a common 
>> pattern that it justifies the change. Seems like many other 
>> languages reached that conclusion as well.
>> Also take a look at a more realistic example with some more 
>> formatting and it will be more obvious (at least to me it is 
>> :) )
>>
>> "The date is " ~ format("%04d", year)) ~ " and " ~ user ~ " 
>> just logged into " ~ here;
>>
>> $"The date is {%04d year} and {user} just logged into {here}"
>
> I see.
> So you want to build format strings as well.
> This is going to be nasty, and likely to complex for a robust 
> implementation.
> Here is what I would support:
>
> String interpolation literals can only be used with strings.
> And they need to start with some prefix which is not an 
> operator.
>
> I"The date is %dateString and the time is %timeString"

I'm talking about building format strings just yet... I'm just 
working with the suggestion that Walter brought up with 
converting the interpolated string into something that can be fed 
into format e.g.:

$"The date is {%04d year} and {user} just logged into {here}"

is rewritten by the compiler to:

"The date is %04d and %s just logged into %s", year, user, here

which can be fed into for example format(). Not sure I like the 
need to call format to get the resulting string, but just working 
with the idea here.

I also think it would loose a lot of value to only allow strings 
as you suggest (e.g. %dateString).
















More information about the Digitalmars-d mailing list