On 03/01/2011 07:58 AM, Peter Lundgren wrote:
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?
Not sure because I never use string mixins, but I guess the answer is precisely
what the error says. Why don't you believe it?
makePossibleAValues() obviously returns a runtime value, so mixin() cannot
evaluate it, I guess.
Denis
--
_________________
vita es estrany
spir.wikidot.com