DIP 1027---String Interpolation---Community Review Round 1

Tim tim.dlang at t-online.de
Sat Dec 14 10:39:07 UTC 2019


On Wednesday, 11 December 2019 at 09:52:21 UTC, Mike Parker wrote:
> This is the feedback thread for the first round of Community 
> Review for DIP 1027, "String Interpolation":
>
> https://github.com/dlang/DIPs/blob/148001a963f5d6e090bb6beef5caf9854372d0bc/DIPs/DIP1027.md

If a special type is used for the format string after lowering, 
it can work with printf and other functions can use the format 
string at compile time:

struct __Interpolate
{
     immutable char *fmt;
     alias fmt this;
}

struct __InterpolateT(string fmt_)
{
     enum fmt = fmt_;
     enum __Interpolate interpolate = __Interpolate(fmt_);
     alias interpolate this;
}

void main()
{
     // printf can be used, because __InterpolateT converts to 
immutable(char)*:
     import core.stdc.stdio;
     printf(__InterpolateT!"test printf %d\n"(), 123);

     // Functions can detect an interpolated string using an 
overload:
     void writef(T...)(__Interpolate fmt, T params)
     {
         printf(fmt.fmt, params);
     }
     writef(__InterpolateT!"test writef %d\n"(), 123);

     // Functions can interpret the format string at compile time:
     void writefct(string fmt, T...)(__InterpolateT!fmt 
interpolate, T params)
     {
         pragma(msg, "format string at compile time ", fmt);
         printf(fmt, params);
     }
     writefct(__InterpolateT!"test writefct %d\n"(), 123);
}



More information about the Digitalmars-d mailing list