String Interpolation Compare - DIP1027 and YAIDIP
Paul Backus
snarwin at gmail.com
Sat Oct 21 19:58:39 UTC 2023
On Saturday, 21 October 2023 at 17:47:52 UTC, Steven
Schveighoffer wrote:
> On Saturday, 21 October 2023 at 06:57:30 UTC, Walter Bright
> wrote:
>> 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:+"
>>
>> ```d
>> 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, ".");
>> }
>> ```
>
> Oh I see now what you are looking at! This is just like C++
> though.
>
> ```c++
> std::cout << "value in hex: " << std::hex << 42
> << ", value in decimal: " << std::dec << 42 <<
> std::endl;
> ```
>
> `dec` is not a function that gets called on the value, it's a
> manipulator to set a flag that says "now do decimal numbers".
> It's no different for this example use case.
I think the misleading part of this example is that it uses
`writeln`. Currently, `writeln` does not support passing this
kind of "stream manipulator" as an argument--even if you write
the lowered version out by hand, it will not work the way the
example implies.
Probably the easiest fix is to replace `writeln` with a made-up
function like `stream.write`.
More information about the Digitalmars-d
mailing list