DIP 1027--String Interpolation--Final Review Discussion Thread
Dennis
dkorpel at gmail.com
Fri Feb 7 22:48:03 UTC 2020
On Wednesday, 5 February 2020 at 05:26:30 UTC, Walter Bright
wrote:
> The reason for formatted strings is for formatting that is
> other than the default, and to interface with common functions
> that use formatted strings.
So the proposed design makes it impossible for a library to
verify arguments are correctly corresponding to format
specifiers, not even at runtime. The programmer is expected to
know the format conventions of the function.
Considering that, what is the purpose of putting the format
specifier inside brackets {}?
Take for example this format string using range formatting
specifiers from Phobos:
```
byte[] arr = [0x10, 0x1A, 0x1F];
writefln("arr = ${%(%X; %)}arr"); // entire format specifier in {}
writefln("arr = %(${%X}arr; %)"); // only element specifier in {}
```
The %( %) format specifiers are not the most beautiful to begin
with, but the mandatory {} make it only worse. I would prefer to
write:
```
writefln("The array is %(%s; %)$arr");
```
It's not like the {} add any delimiting / error checking of any
kind, so I might as well put the format specifier in the string
myself. This would also be required if the format string itself
contained unbalanced {} in them, since no means of escaping { is
provided.
But this still would not work with the current proposal, since a
stray %s will be written into the string because of the absence
of {}. Which, as mentioned before, is a direct contradiction with
the design goal "the language does not now anything about format
specifiers".
One idea is to just write nothing instead of %s, and removing the
{}, which would be less typing and remove the hard-wired Phobos
convention from the language.
Of course, with Steven/Adam's proposal, this can be made to work:
```
writefln("arr = ${X; }arr");
```
Here the {} can actually do the delimiting instead of %( and %),
and possibly do error checking as well!
> 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");
That second one is more prone to mistakes with whitespace, e.g.:
> text("a is", a, ", b is ", b, " and the sum is: ", a + b);
From:
https://forum.dlang.org/post/jahvdekidbugougmyhgb@forum.dlang.org
More information about the Digitalmars-d
mailing list