DIP 1027--String Interpolation--Final Review Discussion Thread

Steven Schveighoffer schveiguy at gmail.com
Wed Feb 5 15:40:59 UTC 2020


On 2/5/20 12:26 AM, Walter Bright wrote:
> On 2/4/2020 1:59 PM, Steven Schveighoffer wrote:
>> On 2/4/20 2:12 AM, Walter Bright wrote:
>>> On 2/3/2020 5:06 PM, Jonathan Marler wrote:
>>>> Some applications want the "Strings and Expressions" form rather 
>>>> than the "Format String" form.
>>>
>>> To have "strings and expressions" then write it that way:
>>>
>>>      foo("I want ", i, " bananas");
>>
>> To have "formatted string output", just write it that way:
>>
>> writefln("I have %s apples and %d bananas", apples, bananas);
> 
> The reason for formatted strings is for formatting that is other than 
> the default, and to interface with common functions that use formatted 
> strings.

In C, the reason for formatting string is because you don't know the 
types of what's passed in! There is no "default".

We have adopted that mechanism in D because another nice feature is to 
be able to read the format of what you expect the result to be without 
all the comma-quote noise. But it's totally not necessary for formatting...

> 
> Strings and expressions are stuck with the default format. If you're 
> happy with the default formatting,
> 
>      writefln("I want $i bananas");
> 
> is not much of any improvement over:
> 
>      writeln("I want ",i," bananas");
> 

They aren't stuck:

writeln("I want ", someFloat.fmt("%0.2f"), " bananas");

We don't even need to change writeln to make this work today. But who 
would rather write that than a string interpolation tuple?

The point of my original (admittedly snarky, sorry) reply to your 
rebuttal here is that your logic can be used to completely dismiss the 
whole string interpolation proposal. Why make the compiler more 
complicated when you can just "write it that way"?

The answer is: because the other way is easier to write, easier to 
maintain, easier to read and overall improves the lives of developers 
everywhere. Syntax sugar is never necessary, but when it makes a 
difference it's a huge factor in the way it feels to use a language. I 
don't think we should dismiss features just because they can be written 
a different way in the existing language.

-Steve


More information about the Digitalmars-d mailing list