www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mixins: to!string cannot be interpreted at compile time

reply Peter Lundgren <lundgrpb rose-hulman.edu> writes:
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?
Feb 28 2011
next sibling parent reply spir <denis.spir gmail.com> writes:
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
Mar 01 2011
parent David Nadlinger <see klickverbot.at> writes:
On 3/1/11 11:45 AM, spir wrote:
 makePossibleAValues() obviously returns a runtime value, so mixin()
 cannot evaluate it, I guess.
Nope, that's not the problem due to CTFE (compile time function execution). David
Mar 01 2011
prev sibling next sibling parent reply David Nadlinger <see klickverbot.at> writes:
On 3/1/11 7: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
 […]
Maybe I missed something as well, but to me it seems to be a CTFE bug, because if you split the offending line into two parts, it result ~= to!string(i); result ~= ", "; Please consider filing this to Bugzilla. David
Mar 01 2011
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
David Nadlinger:

 Maybe I missed something as well, but to me it seems to be a CTFE bug, 
 because if you split the offending line into two parts, it
 
 result ~= to!string(i);
 result ~= ", ";
 
 Please consider filing this to Bugzilla.
Wow. I was wrong, and you are right. It's not a problem of to!() then. Bye, bearophile
Mar 01 2011
parent bearophile <bearophileHUGS lycos.com> writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5671
Mar 01 2011
prev sibling parent reply Peter Lundgren <lundgrpb rose-hulman.edu> writes:
That worked, thanks. This is interesting because the example used in "The D
Programming Language" on page 83 gets away with it just fine. I had no problem
running this:

result ~= to!string(bitsSet(b)) ~ ", ";
Mar 01 2011
next sibling parent Caligo <iteronvexor gmail.com> writes:
On Tue, Mar 1, 2011 at 1:15 PM, Peter Lundgren <lundgrpb rose-hulman.edu>wrote:

 That worked, thanks. This is interesting because the example used in "The D
 Programming Language" on page 83 gets away with it just fine. I had no
 problem
 running this:

 result ~= to!string(bitsSet(b)) ~ ", ";
How did you get that example on page 83 to compile? I'm getting "undefined identifier bitsSet", and it's not in std.intrinsic or std.bitmanip.
Mar 11 2011
prev sibling parent Caligo <iteronvexor gmail.com> writes:
On Fri, Mar 11, 2011 at 11:48 AM, Caligo <iteronvexor gmail.com> wrote:

 On Tue, Mar 1, 2011 at 1:15 PM, Peter Lundgren <lundgrpb rose-hulman.edu>wrote:

 That worked, thanks. This is interesting because the example used in "The
 D
 Programming Language" on page 83 gets away with it just fine. I had no
 problem
 running this:

 result ~= to!string(bitsSet(b)) ~ ", ";
How did you get that example on page 83 to compile? I'm getting "undefined identifier bitsSet", and it's not in std.intrinsic or std.bitmanip.
nvm, it's right there on that very page.
Mar 11 2011
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
Peter Lundgren:

 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
Currently to!() can't run at compile-time. Are you sure you need string mixings? Isn't compile-time run of functions enough for you? If you really want to create that at compile-time as string, you are able to use the ToStringNow, converting makePossibleAValues into a recursive template, and running coprime() at compile-time from the template. But it will eat lot of RAM at compile-time. Bye, bearophile
Mar 01 2011