nested enum like template generator

JS js.mdnq at gmail.com
Mon Jul 15 22:51:51 PDT 2013


On Tuesday, 16 July 2013 at 04:37:33 UTC, Ali Çehreli wrote:
> On 07/15/2013 08:43 PM, JS wrote:
>
> > http://dpaste.dzfl.pl/7c8b0ba9
> >
> > Why the heck can't we use integers in ctfe's? There seems to
> be no
> > simple way to create a counter and this is one of the most
> basic
> > programming constructs to use.. yet with ctfe's it's
> impossible.
> >
> > I'd like each variable in the nested structs to be
> incremented properly.
>
> I did not read the code but just from your description, the 
> separate compilation model that D uses would preclude that. For 
> example, compilation of a.d would not know anything about the 
> counter that b.d has counted during its compilation.
>
> Ali

Huh? what is a.d and b.d?

The template transforms a string into a D code string... which is 
then string mixed in. The counter and code generation have 
nothing to do with the result. A counter is needed in the 
generation of the code to generate enum like characteristics.

It does use recursion, and I could pass a variable that counts 
the number of elements but this too would probably not work due 
to D bitching about using ints.

I imagine one could hack D to make it's flawed CTFE system work, 
to some degree... like using strings:


template inc(string s)
{
     string _(string a)
	{
		if (a == "0") return "1";
		if (a == "1") return "2";
		if (a == "2") return "3";
		if (a == "3") return "4";
		return "0";
	}
	enum inc = _(s); pragma(msg, ":"~_(s));
}

which is a modulo 4 incrementer. So, CTFE's have the ability to 
count... but extremely short sighted that they don't. I imagine 
can write a whole template library using strings to emulate ints 
in ctfe's... what a shame though...


More information about the Digitalmars-d-learn mailing list