compile-time variables?

vsb v04bvs at gmail.com
Fri Jun 1 14:41:44 PDT 2007


May be this helps you.

char[] int2str(int n) {
    if (n == 0)
	return "0";

    char[] result = "";
    int m = 1;
    while (m <= n)
	m *= 10;
    m /= 10;
    while (m > 0) {
	result ~= '0' + n / m;
	n %= m;
	m /= 10;
    }

    return result;
}

char[] unique_var(char[] type, char[] pref, int n) {
    char[] name = pref ~ int2str(n);

    return
	`static if (is(typeof(` ~ name ~ `))) { `
	`    mixin(unique_var("` ~ type ~ `", "` ~ pref ~ `", ` ~ int2str(n + 1) ~ `));`
	`}`
	`else {`
	`    ` ~ type ~ ` ` ~ name ~ `;`
	`}`;
}


void main()
{
    mixin(unique_var("int", "n", 0));
    n0 = 1;
    mixin(unique_var("int", "n", 0));
    n1 = 2;
}



More information about the Digitalmars-d mailing list