String Interpolation Compare - DIP1027 and YAIDIP
Walter Bright
newshound2 at digitalmars.com
Sat Oct 21 06:57:30 UTC 2023
On 10/20/2023 9:54 PM, Steven Schveighoffer wrote:
> I'm not sure I understand this. What is `dec()`?
From https://github.com/John-Colvin/YAIDIP :
"Consider for example using stream manipulators such as dec and hex for writeln
by using an i-string:+"
```
void fun(int x) {
writeln(i"$dec$x in hexadecimal is 0x$hex$x.");
// Lowering: --->
// writeln(.object.imported!"core.interpolation".InterpolationHeader!("",
"dec", "", "x", " in hexadecimal is 0x", "hex", "", "x", ".")(),
// "", dec, "", x, " in hexadecimal is 0x", hex, "", x, ".");
}
```
And:
"There is no need for defining, implementing, and memorizing a sui generis
mini-language of encoded format specifiers"
I wasn't making false statements about that, either.
>> https://www.digitalmars.com/d/archives/digitalmars/D/Just_another_example_of_missing_string_interpolation_370542.html#N370696
>
> mysql requires a string as the sql for the prepared statement. Basically, you
> pass a string with a different type of placeholder specifier "?". It does not
> accept "%s". This is not something I have any control over.
See my example. There's no reason it cannot be extended to replace the %s with
whatever is needed. Also, DIP1027 provides the { } syntax enabling insertion of
whatever placeholder specifier is needed.
> So naturally, since you only get a runtime format string from DIP1027, you need
> to create an equivalent runtime string to pass to the library. How can you do
> that without allocations?
The { } syntax. But even if you needed an allocation, a malloc/free pair will
suffice. The problem with dec() is dec() returns an allocated string, so the
free gets messy.
>> But even if you did rewrite it, it doesn't escape the template function, and
>> can be RAII'd.
>
> That is not comparable to building the correct string at compile-time. It also
> requires *parsing* the format string at runtime.
I'd agree that parsing it is a last resort if the { } also fails. But having
implemented formatted writes, I can attest that parsing it is not a big deal.
It's scanning till you see the %, then substitute your own version. Of course,
using { } means the compiler does it for you at compile time.
More information about the Digitalmars-d
mailing list