Feedback Wanted on Homegrown @nogc WriteLn Alternative

monarch_dodra via Digitalmars-d digitalmars-d at puremagic.com
Fri Oct 3 04:15:28 PDT 2014


On Thursday, 2 October 2014 at 23:32:32 UTC, H. S. Teoh via 
Digitalmars-d wrote:
> Alright, today I drafted up the following proof of concept:
>
> [...]
> 
> writefln!"Number: %d Tag: %s"(123, "mytag");

I had (amongst with others) thought about the possibility of 
"ct-write".

I think an even more powerful concept, would rather having a 
"ct-fmt" object dirctly. Indeed writefln!"string" requires the 
actual format at compile time, and for the write to be done.

It can't just validate that *any* arbitrary (but pre-defined) 
string can be used with a certain set of write arguments.

I'm thinking:

//----
//Define several format strings.
auto english = ctFmt!"Today is %1$s %2$s";
auto french  = ctFmt!"Nous sommes le %2$s %1$s";

//Verify homogeneity.
static assert(is(typeof(english) == typeof(french)));

//Chose your format.
auto myFmt = doEnglish ? english : french;

//Benefit.
writfln(myFmt, Month.oct, 3);
//----

I think this is particularly relevant in that it is these kinds 
of cases that are particularly tricky and easy to get wrong.



For "basic" usage, you'd just use:
writefln(ctFmt!"Number: %d Tag: %s", 123, "mytag");

The hard part is finding the sweet spot in runtime/compile time 
data, to make those format strings runtime-type compatible. But 
it should be fairly doable.


More information about the Digitalmars-d mailing list