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 next sibling parent 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
prev sibling next sibling parent Caligo <iteronvexor gmail.com> writes:
--20cf307f35ba18f31e049e3893fb
Content-Type: text/plain; charset=ISO-8859-1

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. --20cf307f35ba18f31e049e3893fb Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Tue, Mar 1, 2011 at 1:15 PM, Peter Lu= ndgren <span dir=3D"ltr">&lt;<a href=3D"mailto:lundgrpb rose-hulman.edu">lu= ndgrpb rose-hulman.edu</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_= quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, = 204, 204); padding-left: 1ex;"> That worked, thanks. This is interesting because the example used in &quot;= The D<br> Programming Language&quot; on page 83 gets away with it just fine. I had no= problem<br> running this:<br> <br> result ~=3D to!string(bitsSet(b)) ~ &quot;, &quot;;<br> </blockquote></div><br><br>How did you get that example on page 83 to compi= le?=A0 I&#39;m getting &quot;undefined identifier bitsSet&quot;, and it&#39= ;s not in std.intrinsic or std.bitmanip.<br> --20cf307f35ba18f31e049e3893fb--
Mar 11 2011
prev sibling parent Caligo <iteronvexor gmail.com> writes:
--bcaec51b9bf5bc1223049e397678
Content-Type: text/plain; charset=ISO-8859-1

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. --bcaec51b9bf5bc1223049e397678 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Fri, Mar 11, 2011 at 11:48 AM, Caligo= <span dir=3D"ltr">&lt;<a href=3D"mailto:iteronvexor gmail.com">iteronvexor= gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style= =3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); p= adding-left: 1ex;"> <div><div></div><div class=3D"h5"><br><br><div class=3D"gmail_quote">On Tue= , Mar 1, 2011 at 1:15 PM, Peter Lundgren <span dir=3D"ltr">&lt;<a href=3D"m= ailto:lundgrpb rose-hulman.edu" target=3D"_blank">lundgrpb rose-hulman.edu<= /a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> That worked, thanks. This is interesting because the example used in &quot;= The D<br> Programming Language&quot; on page 83 gets away with it just fine. I had no= problem<br> running this:<br> <br> result ~=3D to!string(bitsSet(b)) ~ &quot;, &quot;;<br> </blockquote></div><br><br></div></div>How did you get that example on page= 83 to compile?=A0 I&#39;m getting &quot;undefined identifier bitsSet&quot;= , and it&#39;s not in std.intrinsic or std.bitmanip.<br> </blockquote></div><br>nvm, it&#39;s right there on that very page.<br> --bcaec51b9bf5bc1223049e397678--
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