Scriptlike v0.9.4 - Perl-like interpolated strings, full examples and more.

H. S. Teoh via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Thu Sep 24 17:15:48 PDT 2015


On Thu, Sep 24, 2015 at 08:00:53PM -0400, Andrei Alexandrescu via Digitalmars-d-announce wrote:
[...]
> Yah, I think we need something like that in the stdlib. Also, we need
> writefln with compile-time format string (someone was working on it
> but I haven't heard about it in a while). -- Andrei

https://issues.dlang.org/show_bug.cgi?id=13568

I wanted to work on it, but haven't actually gotten to it yet.
Basically, the idea is relatively simple:

	// compile-time variant
	void writefln(string format="", A...)(A args)
		if (format.length > 0)
	{
		... // implementation here
	}

	// runtime variant
	void writefln(string format="", A...)(A args)
		if (format.length == 0 && args.length > 0 &&
			is(typeof(args[0]) == string))
	{
		... // current implementation
	}

This will allow backward compatibility with the current writefln API,
while allowing existing code to simply transition from:

	writefln("...", ...);

to:

	writefln!"..."(...);

The tricky part is how to extricate the various parts of the current
implementation in order to take full advantage of compile-time format
strings, e.g., (1) don't import anything except what's necessary to
format the current format string (the current format() has to import,
e.g., std.bigint even if you never use BigInt, because it can't assume
that a runtime format string might not ask to format BigInts; this
causes the infamous format() template bloat) -- this includes allowing
format() to be pure, @nogc, etc. if your format strings never ask for
anything that requires that; (2) compile-time argument mismatch checking
(e.g., writefln!"%d"("abc") should give a compile-time error rather than
a runtime exception).

A lot of the current implementation will probably have to be overhauled
/ rewritten in order to make this work. That part unfortunately requires
a lot of time, which I don't have right now.


T

-- 
Creativity is not an excuse for sloppiness.


More information about the Digitalmars-d-announce mailing list