Mixins: to!string cannot be interpreted at compile time

Peter Lundgren lundgrpb at rose-hulman.edu
Mon Feb 28 22:58:15 PST 2011


I'm trying to use mixins to generate an array of numbers that are coprime to a
statically known value. I've tried the following, but I receive the error:

Error: to(i) ~ ", " cannot be interpreted at compile time


string makePossibleAValues(string name, byte m) {
	string result = "immutable byte[] "~name~" = [";
	foreach (i; 0 .. m) {
		if (coprime(i, m)) {
			result ~= to!string(i) ~ ", ";
		}
	}
	return result ~ "];";
}

bool coprime(ulong a, ulong b) {
	return gcd(a, b) == 1;
}

ulong gcd(ulong a, ulong b) {
	while (b) {
		auto t = b;
		b = a % b;
		a = t;
	}
	return a;
}

mixin(makePossibleAValues("aValues", 26));


makePossibleAValues("aValues", 26) produces the correct result, "immutable
byte[] aValues = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25, ];", at runtime
and I know to!string can be used in mixins. Any idea as to why this particular
code is having trouble with to!string?


More information about the Digitalmars-d-learn mailing list