Feedback Wanted on Homegrown @nogc WriteLn Alternative

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Thu Oct 2 18:55:41 PDT 2014


On Fri, Oct 03, 2014 at 12:04:23AM +0000, bearophile via Digitalmars-d wrote:
> H. S. Teoh:
> 
> >So this new syntax can be implemented alongside the existing syntax
> >and people can gradually migrate over from purely-runtime format
> >strings to compile-time, statically-checked format strings.
> 
> Very good.
> 
> D has a static type system, unlike Python/Ruby/etc but D printing
> functions use dynamic typing for format strings. This is just wrong.
> 
> There is a template bloat problem, it's not a big problem, but I'd
> like some way to use template arguments that are only used to run
> compile-time functions to test them, and then leave zero template
> bloat behind :-) This was one of the purposes of a "enum
> precondition".
[...]

The way I envision it, the eventual implementation of formattedWrite
with CT format string will basically decompose every format string +
arguments call into a series of calls to individual formatting
functions. For example:

	writefln!"You have %d items in mailbox %s"(n, mboxName);

would get translated at compile time into the equivalent of:

	write("You have ");
	formattedWrite("%d", n);
	write(" items in mailbox ");
	formattedWrite("%s", mboxName);

The idea being that a call like `formattedWrite("%d", n)` is far more
likely to be reused in other places in the program, than the specific
format string "You have %d items in mailbox %s" and that specific
combination of parameter types (int, string). So even though this does
incur some template bloat, it will hopefully break down the template
instantiations into smaller chunks that are frequently reused.


T

-- 
Lottery: tax on the stupid. -- Slashdotter


More information about the Digitalmars-d mailing list