String Interpolation Compare - DIP1027 and YAIDIP

Steven Schveighoffer schveiguy at gmail.com
Sat Oct 21 03:51:46 UTC 2023


On Saturday, 21 October 2023 at 02:22:49 UTC, Adam D Ruppe wrote:
> On Saturday, 21 October 2023 at 01:50:33 UTC, Walter Bright 
> wrote:
>> 2. It's inefficient because it requires the arguments to be 
>> converted to string temporaries, and then the strings are 
>> appended to the result.
>
> Not true. You have shown zero understanding of what it is, even 
> though there's an implementation you could look at in addition 
> to the document!

You can even call printf with betterC (no runtime memory 
allocation whatsoever, not even malloc) with introspection of 
parameters, something that 1027 *cannot do*.

>
>>     i"axy:  $leadingZero$digits(3)$a $scientific$x 
>> $fixed(20,10)$y" // YAIDIP
>
> This is one possible use case of it, but there's several 
> others, including using format strings. It is 100% up to the 
> library what to do; the compiler is agnostic to these specifics.

YAIDIP:

```d
i"axy:  $a{03} $x{e} $y{20.10}"
```

Compare that to dip 1027 (how verbose!)

```d
i"axy:  ${%03d}a ${%e}x ${%20.10f}y"
```

>> 5. The tuple output, which the user is sooner or later going 
>> to be confronted with, looks like (copied from YAIDIP):
>
> This is half true. We could make the compiler error messages 
> nicer with a bit of implementation effort, but currently they 
> messages do indeed look like:
>
> ```
> test.d(51): Error: function `test.foos(string s)` is not 
> callable using argument types `(InterpolationHeader, 
> InterpolatedLiteral!"Hello, ", InterpolatedExpression!"name", 
> string, InterpolatedLiteral!"", InterpolationFooter)`
> test.d(51):        cannot pass argument `InterpolationHeader()` 
> of type `InterpolationHeader` to parameter `string s`
> ```

YAIDIP is here to help!

```
Perhaps use `std.conv.text` to make a string from an 
interpolation tuple?
```

And of course we can clean up the parameter types to be more 
readable (we already do this for `string`).

>> 8. There will be difficulties using this with betterC because 
>> of the required string allocations mentioned earlier.
>

How can you possibly think that YAIDIP requires allocations? The 
only proposal that requires allocations is DIP1027, since you 
must rebuild the format string if your function doesn't support 
printf style format specifiers (i.e. mysql). You have it exactly 
backwards.

>> # DIP1027
>>
>> # Pros
>>
>> 3. does not require a library of manipulators
>
> Not true - that library is `format` or `printf` etc., but it is 
> there.

Not even printf works as expected, only writef.

Try doing printf with a string parameter (not a string literal) 
with DIP1027.

---

I want to stress that I'm not in love with YAIDIP. If we want to 
go with a templated interpolation tuple, I'd wish for 1036, but 
without the auto-string conversion.

-Steve


More information about the Digitalmars-d mailing list