The state of string interpolation

Atila Neves atila.neves at gmail.com
Fri Dec 7 17:11:35 UTC 2018


On Thursday, 6 December 2018 at 00:10:56 UTC, o wrote:
> I really wish that D had string interpolation, and I can see 
> that a ton of other people also do. String interpolation has 
> been brought up before, but it has been ignored/put down every 
> time. Jonathan Marler even created a Pull Request 
> (https://github.com/dlang/dmd/pull/7988) to add this feature, 
> but he ended up closing it because it was getting nowhere. The 
> excuse that keeps being given for not adding this feature is 
> something along the lines of "There are ways to work around 
> this, so it is unnecessary to add it". If this is the case, 
> then why do people keep on requesting it again and again? Here 
> are just some cases of other people asking for string 
> interpolation:
>
> - https://forum.dlang.org/thread/c2q7dt$67t$1@digitaldaemon.com
> - 
> https://forum.dlang.org/thread/qpuxtedsiowayrhgyell@forum.dlang.org
> - 
> https://forum.dlang.org/thread/ncwpezwlgeajdrigegee@forum.dlang.org
> And there are 2 closed PRs for this:
> - https://github.com/dlang/dmd/pull/6703
> - https://github.com/dlang/dmd/pull/7988
>
> In my opinion, having a sizable amount of people request a 
> feature (about 100 out of 280 as of The State of D Survey 
> [https://dlang.typeform.com/report/H1GTak/PY9NhHkcBFG0t6ig]) is 
> a good enough to realize that maybe we should start thinking of 
> adding it to the language.
>
> I understand that this may involve a DIP process, which takes 
> time. Therefore I am willing to offer up $100 to someone if 
> they are willing to go through the DIP process for string 
> interpolation.
>
> And please don't mention Phobos' sorry excuse for string 
> interpolation:
> "a is: %s, b is: %s, sum is %s.".format(a, b, a + b)
> Is no better than
> "a is: "~a.to!string~"b is: "~b.to!string~" and the sum is: 
> "~(a+b).to!string~"."

It's better, the latter is much worse. Longer, harder, to read, 
uglier. AFAIC `.to!string` is an anti-pattern.

> This is just so much more readable, and maintainable:
> "a is: ${a}, b is: ${b}, and the sum is: ${a+b}."

So is this:

text("a is", a, ", b is ", b, " and the sum is: ", a + b);

Which works today.

The only use I'd have for string interpolation is when generating 
code (as a string) that spans multiple lines. Before this thread 
I wasn't aware that scriptlike had string interpolation. Now that 
I know it does, I don't know what I'd need "proper" (i.e. 
language assisted) SI.

Every language change is a cost, and therefore should justify its 
inclusion. I personally don't think that it is in this case just 
to make a niche use case slightly easier, and this coming from 
someone from that niche! From now on it's:

const code = mixin(interp!q{
     // stuff that I used to write with many %s and it was hard to 
match them up
});


More information about the Digitalmars-d mailing list