Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 17, 2012 variable x cannot be read at compile time (ctfe) | ||||
---|---|---|---|---|
| ||||
I finally got around trying to finish my sudoksolver and I'm pretty happy with the result, except one little piece that screams for ctfe but I never seem to get it working. I always get "variable x cannot be read at compile time" in this method : auto bitsetToRange(in SudokuCell x) { return boardSide.iota().filter!(i => (x >> i) & 1)().map!(x=>x+1)(); } with SudokuCell beeing declared as: alias ushort SudokuCell; I want to generate a dynamic array with all return values form this method with x ranging from 0..sudokuCell.size. Why is it complaining x cannot be read at compile time while it is passed at compile time? |
December 17, 2012 Re: variable x cannot be read at compile time (ctfe) | ||||
---|---|---|---|---|
| ||||
Posted in reply to maarten van damme | On Monday, 17 December 2012 at 09:42:50 UTC, maarten van damme wrote:
> I finally got around trying to finish my sudoksolver and I'm pretty
> happy with the result, except one little piece that screams for ctfe
> but I never seem to get it working.
> I always get "variable x cannot be read at compile time" in this method :
>
> auto bitsetToRange(in SudokuCell x) {
> return boardSide.iota().filter!(i => (x >> i) & 1)().map!(x=>x+1)();
> }
>
> with SudokuCell beeing declared as:
> alias ushort SudokuCell;
>
> I want to generate a dynamic array with all return values form this
> method with x ranging from 0..sudokuCell.size.
> Why is it complaining x cannot be read at compile time while it is
> passed at compile time?
Probably because you declared "boardside" as an "auto"? You need to declare it as immutable or enum. Can't add much to it without some context.
FYI: As much as I love ufcs, "boardSide.iota()" makes no sense to me. It took me a minute to realize what was going on. I'd really write it as:
return iota(boardSide).filter!(i => (x >> i) & 1)().map!(x=>x+1)();
|
December 17, 2012 Re: variable x cannot be read at compile time (ctfe) | ||||
---|---|---|---|---|
| ||||
Posted in reply to maarten van damme | maarten van damme:
> I always get "variable x cannot be read at compile time" in this method :
>
> auto bitsetToRange(in SudokuCell x) {
> return boardSide.iota().filter!(i => (x >> i) & 1)().map!(x=>x+1)();
> }
>
> with SudokuCell beeing declared as:
> alias ushort SudokuCell;
Why don't you show us a little complete compilable program that contains your problem?
Bye,
bearophile
|
December 17, 2012 Re: variable x cannot be read at compile time (ctfe) | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Here is a trimmed down version : http://dpaste.dzfl.pl/11170641 thanks for the quick reply. |
December 17, 2012 Re: variable x cannot be read at compile time (ctfe) | ||||
---|---|---|---|---|
| ||||
Posted in reply to maarten van damme | maarten van damme: > Here is a trimmed down version : http://dpaste.dzfl.pl/11170641 > > thanks for the quick reply. It seems to work if you move ".array()" from generateBitsetCache() to bitsetToRange(). I think it's a problem of nested delegates at CT. But if this is the problem, dmd used to give a more clear error message. > both cache~=bitsetToRange(x).array(); > MaxArray!(int, boardSide) temp=bitsetToRange(x).array(); > fail ... I think you need to define more operators in MaxArray to allow some of that. Bye, bearophile |
December 17, 2012 Re: variable x cannot be read at compile time (ctfe) | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > I think it's a problem of nested delegates at CT. But if this is the problem, dmd used to give a more clear error message. Two minimized programs that show the problems: ------------- import std.algorithm: filter; import std.array: array; auto foo(in int x) { return [1, 2].filter!(i => x)(); } enum a = foo(3).array(); void main() {} temp.d(4): Error: variable x cannot be read at compile time ------------- import std.array: array; enum a = [1, 2].array(); void main() {} ...\src\druntime\import\core\memory.d(251): Error: gc_malloc cannot be interpreted at compile time, because it has no available source code ------------- Bye, bearophile |
December 17, 2012 Re: variable x cannot be read at compile time (ctfe) | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Thanks, I'm really happy it works now.
it's odd that the resulting arraylist is slower to access at runtime then the arraylist generated at runtime... Is there a reason for this?
Should I open bugreports for your testcases?
2012/12/17 bearophile <bearophileHUGS@lycos.com>:
>> I think it's a problem of nested delegates at CT. But if this is the problem, dmd used to give a more clear error message.
>
>
> Two minimized programs that show the problems:
>
> -------------
>
> import std.algorithm: filter;
> import std.array: array;
> auto foo(in int x) {
> return [1, 2].filter!(i => x)();
> }
> enum a = foo(3).array();
> void main() {}
>
>
>
> temp.d(4): Error: variable x cannot be read at compile time
>
> -------------
>
> import std.array: array;
> enum a = [1, 2].array();
> void main() {}
>
>
>
> ...\src\druntime\import\core\memory.d(251): Error:
> gc_malloc cannot be interpreted at compile time, because it has no available
> source code
>
> -------------
>
> Bye,
> bearophile
|
December 17, 2012 Re: variable x cannot be read at compile time (ctfe) | ||||
---|---|---|---|---|
| ||||
Posted in reply to maarten van damme | maarten van damme: > it's odd that the resulting arraylist is slower to access at runtime > then the arraylist generated at runtime... Is there a reason for this? In such cases, beside thinking some time about the topic, one solution is to take a look at the asm. Maybe it's the same problem as with enum arrays, they get copied like literals every time you use them. > Should I open bugreports for your testcases? I think the secondo program, that just calls array(), doesn't show a problem that's meant to be fixed. Regarding the first with filter, I think it's a known temporary limitation of CTFE (caused by nested lambdas). I am not sure because DMD used to give a more specific error message for this limitation. So I think they don't need to be reported. Bye, bearophile |
December 17, 2012 Re: variable x cannot be read at compile time (ctfe) | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | How do I make dmd output asm?
I wrote a simple test and for a simple int[] array it was 2,5 times slower.
2012/12/17 bearophile <bearophileHUGS@lycos.com>:
> maarten van damme:
>
>
>> it's odd that the resulting arraylist is slower to access at runtime then the arraylist generated at runtime... Is there a reason for this?
>
>
> In such cases, beside thinking some time about the topic, one solution is to take a look at the asm.
>
> Maybe it's the same problem as with enum arrays, they get copied like literals every time you use them.
>
|
December 17, 2012 Re: variable x cannot be read at compile time (ctfe) | ||||
---|---|---|---|---|
| ||||
Posted in reply to maarten van damme | maarten van damme:
> How do I make dmd output asm?
You can't, unfortunately. They closed this enhancement request of mine because they say DMD is not designed for this.
On Linux there are several disassemblers, I use objdump. On Windows there are other ones.
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation