Why is there no lazy `format`?

rikki cattermole rikki at cattermole.co.nz
Tue Oct 20 13:45:20 UTC 2020


You are describing the purpose of an output range.

I.e.

void test() {
     InPlaceAppender appender;

     appender.formattedWrite!"%d: %d"(123, 456);

     stdout.rawWrite(appender.get);
}

struct InPlaceAppender {
     private {
         char[ushort.max] buffer;
         size_t used;
     }

     @disable this(this);

     void put(char c) {
         assert(used < buffer.length);
         buffer[used++] = c;
     }

     scope char[] get() {
         return buffer[0 .. used];
     }

     void reset() {
        used = 0;
        buffer[] = '\0';
     }
}


More information about the Digitalmars-d mailing list