Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 01, 2011 Mixins: to!string cannot be interpreted at compile time | ||||
---|---|---|---|---|
| ||||
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? |
March 01, 2011 Re: Mixins: to!string cannot be interpreted at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Lundgren | 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 |
March 01, 2011 Re: Mixins: to!string cannot be interpreted at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Lundgren | 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
|
March 01, 2011 Re: Mixins: to!string cannot be interpreted at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | 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
|
March 01, 2011 Re: Mixins: to!string cannot be interpreted at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Lundgren | 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
|
March 01, 2011 Re: Mixins: to!string cannot be interpreted at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | 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
|
March 01, 2011 Re: Mixins: to!string cannot be interpreted at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | http://d.puremagic.com/issues/show_bug.cgi?id=5671 |
March 01, 2011 Re: Mixins: to!string cannot be interpreted at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | 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)) ~ ", "; |
March 11, 2011 Re: Mixins: to!string cannot be interpreted at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Lundgren Attachments:
| 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. |
March 11, 2011 Re: Mixins: to!string cannot be interpreted at compile time | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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.
|
Copyright © 1999-2021 by the D Language Foundation