Discussion Thread: DIP 1036--String Interpolation Tuple Literals--Community Review Round 2
Steven Schveighoffer
schveiguy at gmail.com
Fri Jan 29 15:06:20 UTC 2021
On 1/29/21 3:02 AM, Walter Bright wrote:
> Comparing two simple examples of #DIP1036 and #DIP1027:
>
> DIP1036:
> printf(i"%s${item} %02d${other_item}"); // metaprogramming + printf
> overload
With a metaprogramming wrapper, it would be:
printf(i"${item} %02d${other_item}");
Without a metaprogramming wrapper, you call it like this:
printf("%s %02d", item, other_item); // yes, just use printf the way it
was intended
The point of this is, we don't want to fit into printf-style formatting,
because it's too niche a need, and extremely limiting. Use the wrapper
if you want printf formatting. This is not a burden on anyone (the
wrapper took me 15 minutes to write, and is an inlineable no-processing
call).
>
> DIP2027:
> printf(i"$item ${%02d}other_item"); // direct
This is an error, unless item is a const char * (unlikely).
> DIP1036:
> writeln(i"I ate ${apples} apples and ${bananas} bananas totaling
> ${apples + bananas} fruit.");
>
> DIP1027:
> writefln(i"I ate $apples apples and $bananas bananas totaling
> $(apples + bananas) fruit.");
>
>
> DIP1036 is more user typing, including awkward-to-type { }, along with
> needing substantial user code to implement (for printf).
It is possible for the DIP to allow omission of the {}. We did not do
this for several reasons:
1. Just a literal $ is frequently used in string data (it is a
denomination, and has valid uses in mixin D code).
2. The requirement to type {} is not much of a burden.
3. Using enclosing tokens has precedent in many string interpolation
implementations. Javascript uses this exact sequence. Swift uses \( ...
). C# uses { ... }.
4. It's easier for a person to distinguish in a large string.
>
> DIP1027's syntax is optimized to make the most common cases the simplest
> for the user, no user code is required.
DIP1027's syntax is designed to fit with a few existing functions. It
puts the burden on the user to make sure they understand how the rewrite
will happen, and which functions to use to do this. It is not forgiving
of mistakes, which will compile and do the wrong thing.
But I don't want to rehash DIP1027 here, that discussion has already
happened. If you want to compare the two briefly, I would say DIP1027
designed for writef and format, nothing else. It can be used
unintuitively with printf and other functions that have some similar
parameter layouts. It can be used accidentally with many functions that
happen to accept the parameters as arranged. DIP1036 is designed to be
used by library writers to provide a mechanism to distinguish and handle
properly interpolation strings ONLY when intended, and do it with little
effort, all while also providing the user a mechanism to seamlessly
convert normal data into string data.
-Steve
More information about the Digitalmars-d
mailing list