String Interpolation Compare - DIP1027 and YAIDIP
Walter Bright
newshound2 at digitalmars.com
Sat Oct 21 04:10:35 UTC 2023
On 10/20/2023 8:51 PM, Steven Schveighoffer wrote:
> Not even printf works as expected, only writef.
>
> Try doing printf with a string parameter (not a string literal) with DIP1027.
To use printf with i-strings, it's implicitly necessary to use arguments that
are compatible with printf. That means const(char)* arguments, not string
arguments. This is something that should be obvious once how i-string conversion
to tuples works.
DIP1027 has zero knowledge of `writef` or `printf` or `format`.
--- However ---
Ever since we added printf format checking to the language, I've toyed with the
idea of going a step further and adjusting the arguments (and formats) of the
calls to printf to make them work.
For example, a D string argument `s` would be split into two parameters,
`cast(int)s.length, s.ptr`. The format `%s` would be replaced with `%.*s`. In
other words, you could do things like:
```
printf("integer %s", 67);
```
would be rewritten as:
```
printf("integer %d", 67);
```
After all, if the format checker can give an error message with the correct
format specifier, why not just fix it?
However, this would be independent of string interpolation. It's just that the
two separate initiatives would fit together handsomely when dealing with printf.
More information about the Digitalmars-d
mailing list