The state of string interpolation

Steven Schveighoffer schveiguy at gmail.com
Thu Dec 6 22:04:13 UTC 2018


On 12/6/18 4:20 PM, Mike Franklin wrote:
> On Thursday, 6 December 2018 at 20:00:02 UTC, H. S. Teoh wrote:
>> Now, I'm also thinking about how to minimize the "surface area" of 
>> language change in order to make this proposal more likely to be 
>> accepted by W&A.
> 
> IMO the one thing that will likely get this across the finish line is to 
> generalize it:
> 
> 1) What is the fundamental missing language feature that is preventing 
> us from implementing interpolated strings in the library?
> 
> 2) What other use cases besides string interpolation would be enabled by 
> adding such a feature?
> 
> If a compelling language feature that has more uses beyond string 
> interpolation can be engineered and/or discovered, I think it will have 
> a much better chance of being accepted.

It's really not what's enabled (clearly, things can still be used 
without string interpolation, and we can use mixins to simulate close to 
what we would want).

It's how one writes and reads code. The efficiency and elegance is not 
to be ignored.

I view `foreach` as a fundamental feature of D. Yet, it's completely 
unnecessary, given the existence of `for`. AND given the power of D's 
mixins, we can completely replace the functionality with a Foreach 
library type or function. Despite all that, I'd rather have foreach as a 
language construct, because it's so damned clean.

It's the boiling down of the syntax to the fundamental expression of the 
developer. I want to write the parameters to a function or to a template 
without having to reach for the comma-quote-separator-quote-comma 
sequence. Not only is this noisy and cumbersome, but it breaks up the 
data and the presentation in a way that is difficult to piece together.

writeln(a, " ", b, " ", c, " ", d);

is messy and distracting. It's like a photograph with lots of extraneous 
items taking away from what you should be looking at.

writefln("%s %s %s %s", a, b, c, d);

Better, I can envision how the result comes out. But get that string to 
be long and complicated, or add 5 more parameters, and now it's hard to 
figure out what variable goes where. It's not coupled to the location it 
belongs. Now the photograph has cutouts for your family members, and the 
actual photos of your family scrunched up together on the side, out of 
the picture. You just have to imagine what it would be like if they were 
in there.

writeln("$a $b $c $d");

Simple, elegant, obvious. With syntax highlighting, the symbols are 
identified easily. Its like a photograph with your family in focus 
against a nice background. I literally would have to explain the usage 
of this feature to nobody who is familiar with C. Or even PHP for that 
matter!

So it's not about what it enables, it's about enabling your 
expressiveness, and bringing focus to what is important when writing code.

Now, on top of that, if we do go with the lowering to tuples, we get 
even more power. We have so many more options than just a simple 
concatenated string. The DB query possibility makes my mouth water. And 
I've written a lot of DB queries with D. They are hard to write, and 
hard to read. Every time I add a stupid column, and then I have to add a 
new placeholder, and put the new value in the right place in the 
trailing arguments, it's a long process.

So everything this enables is doable, right now, in a library. It 
wouldn't be nearly as pretty, and the alternatives today without mixins 
are ugly, unfocused, and distracting. I won't say that we absolutely 
have to have this feature in the language, but I would say it would be a 
shining star of a feature when compared with other languages.

-Steve


More information about the Digitalmars-d mailing list