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

mipri mipri at minimaltype.com
Sat Dec 14 23:54:35 UTC 2019


On Saturday, 14 December 2019 at 21:35:51 UTC, Tove wrote:
> Is format strings worth building on?
>
> With UFCS we could create types which control formatting
> "pi=", pi.dec(10),
> 255, " in hex ", 255.hex

This is already possible, though:

   import std;

   struct HexPrinter(T) {
       T item;
       void toString(scope void delegate(string) sink, 
FormatSpec!char fmt) const {
           sink(format("%x", item));
       }
   }

   HexPrinter!T hex(T)(T n) {
       return HexPrinter!T(n);
   }

   void main() {
       writefln("255 = %s hex", 255.hex);
   }

so with DIP-1027 it'd remain possible with a syntax like

   writefln(i"255 = $(255.hex) hex");



More information about the Digitalmars-d mailing list