DIP 1027---String Interpolation---Community Review Round 1

mipri mipri at minimaltype.com
Thu Dec 12 07:30:19 UTC 2019


On Wednesday, 11 December 2019 at 23:33:20 UTC, mipri wrote:
> This is going to function as something like an attractive
> nuisance for people that learn D.
>
> Newbie: "Hey, I want some interpolation here. Does D have that?
> Oh! It
> does!"
>
> Newbie: *uses interpolation, gets error*

I think there's a general rule here:

A DIP that adds an innovative feature, or which enters
poorly-explored design space, can be a conservative "80%
solution" that has some caveats and limitations on its use,
that people learning the language will need to learn to
properly use the feature.

But a DIP that adds a very well explored feature should provide
a 100% solution that adds exactly what people will expect from
"<feature name> in <any language>". It should do this
precisely because people are so well-prepared in their
expectations for any such feature.

Suppose that D didn't have floating point numbers, and a DIP
were released today to add them to the language, but instead of
IEEE floating point, it implemented some novel thing. The
details of the novel thing don't matter: it's not 100% what
people want from "floating point", so it's an embarrassment.

Think of how the documentation would have to read. dlang.org's
own docs, to be useful, would have to explain the feature in
terms of how it fails to meet the expectations of other
languages.

In code, this must all work:

   auto s1 = i"I ate %apples and %{d}bananas.";
   enum pears = "two";
   enum s2 = i"I ate %pears.";
   immutable string s3 = i"%s1";

   void takes_a_normal_string(string s) { }

   takes_a_normal_string(i"%one + %two = %(one + two).");

   // not an error: writefln sees %d in the post-interpolation
   // string and works normally
   writefln(i"making %bread using %%d ingredients", 6);

   string x = qi"SPAM
   Hello %firstname %lastname!
   ...
   SPAM";

Although if i"" is definitely the syntax used, then the
rest of the syntax is free to be something familiar like

   // resembles 14 languages from wikipedia examples
   i"$one + $two = ${one + two}"

or

   // resembles 4 languages
   i"{one} + {two} = {one + two}"

Finally, look at Swift's syntax:

   let apples = 4
   print("I have \(apples) apples")

How about that? Undefined escape sequences are an error, so
new ones can be added without breaking the language or changing
the meaning of old code. And this no longer requires an ugly
'i' prefix.

If the "lower to printf-style arguments" is really what's
wanted, then it could be an *option* with an alternate escape
like \[apples], so people's expectations about string
interpolation would still be met. A 100% solution + some extra
features is a much easier sell.




More information about the Digitalmars-d mailing list