DIP 1027---String Interpolation---Format Assessment
Petar
Petar
Thu Feb 27 17:41:12 UTC 2020
On Thursday, 27 February 2020 at 14:58:20 UTC, Adam D. Ruppe
wrote:
> On Thursday, 27 February 2020 at 14:32:29 UTC, Petar Kirov
> [ZombineDev] wrote:
>> 2. Have the new type implicitly convert to printf-style args.
>> I think this is what Adam is proposing. While nice to have, I
>> don't think it's necessary.
>
> You can read my document for more detail
>
> https://github.com/dlang/DIPs/pull/186
>
> But basically
>
> writefln(i"hi $name, you are visitor ${%2d}(count)");
>
> gets turned into:
>
> writefln(
> // the format string is represented by this type
> new_type!("hi ", spec(null), ", you are visitor ",
> spec("%2d"))(),
> // then the referenced arguments are passed as a tuple
> name,
> count
> )
>
>
> So very, very, very similar to Walter's proposal, just instead
> of the compiler generating the format string as a plain string,
> the format string is represented by a new type, defined by the
> spec and implemented by druntime. As a result, no more guess
> work - it is clear that this is meant to be interpreted as a
> format string. It is clear which parts are
> placeholders/specifiers for which arguments.
Perhaps my assumptions were based on an old version of your
proposal.
What I want is for:
auto s = i"hi $name, you are visitor ${%2d}(count)";
to lower to:
auto s = new_type!(
"hi ", spec(null), ", you are visitor ", spec("%2d")
)(name, count);
I.e. the referenced arguments are passed to the constructor of
new_type.
That way new_type can offer implicit conversion to string, while
support for zero-allocation printf, write, writeln, writef,
writefln and so on can be done via function overloading.
More information about the Digitalmars-d-announce
mailing list