Feedback Wanted on Homegrown @nogc WriteLn Alternative

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 26 22:49:12 PDT 2014


On Sat, Sep 27, 2014 at 05:58:53AM +0200, Andrej Mitrovic via Digitalmars-d wrote:
> On 9/27/14, H. S. Teoh via Digitalmars-d <digitalmars-d at puremagic.com> wrote:
> > Anyway, what I have in mind is more to take the current Phobos
> > std.format (of which writeln is just a thin wrapper), stick @nogc on
> > it, and hack it until it either compiles with @nogc, or isolate the
> > GC parts
> 
> I don't see how, unless you provide an overload taking a buffer.

std.format.formattedWrite takes an output range. The user can pass in a
preallocated buffer that doesn't depend on the GC. The only remaining
question is whether formattedWrite currently has any GC-dependent parts,
and whether those parts are isolatable.

In fact, this might be a good opportunity to introduce a formatting
function that takes a compile-time format string. That would open up the
possibility of compile-time checking of format arguments (that
bearophile has been clamoring for), as well as truly minimal runtime
dependencies, where the only things that get included at runtime are the
pieces necessary to process that particular format string. So if none of
the required pieces are GC-dependent, the entire formatting call will be
@nogc (and the compiler would automatically infer this). Ditto with
pure, @safe, etc..

Today an idea occurred to me, that if we extend the current writefln
(and other similar formatting calls like std.string.format) to have this
signature:

	void writefln(string ctFmt = "", A...)(A args);

then we can adopt the convention that if ctFmt is "", then args[0] will
be interpreted as the (runtime) format string, so existing code will
still work as before (due to IFTI inferring ctFmt as ""), but people can
start rewriting their formatting calls to:

	writefln!"...format string here"(... /* arguments here */);

to take advantage of compile-time analysis and processing of the format
string.


T

-- 
The right half of the brain controls the left half of the body. This means that only left-handed people are in their right mind. -- Manoj Srivastava


More information about the Digitalmars-d mailing list