digitalmars.D.learn - variable x cannot be read at compile time (ctfe)
- maarten van damme (13/13) Dec 17 2012 I finally got around trying to finish my sudoksolver and I'm pretty
- monarch_dodra (10/29) Dec 17 2012 Probably because you declared "boardside" as an "auto"? You need
- bearophile (5/13) Dec 17 2012 Why don't you show us a little complete compilable program that
- maarten van damme (2/2) Dec 17 2012 Here is a trimmed down version : http://dpaste.dzfl.pl/11170641
- bearophile (9/14) Dec 17 2012 It seems to work if you move ".array()" from
- bearophile (20/22) Dec 17 2012 Two minimized programs that show the problems:
- maarten van damme (5/27) Dec 17 2012 Thanks, I'm really happy it works now.
- bearophile (14/19) Dec 17 2012 In such cases, beside thinking some time about the topic, one
- maarten van damme (3/10) Dec 17 2012 How do I make dmd output asm?
- bearophile (7/8) Dec 17 2012 You can't, unfortunately. They closed this enhancement request of
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/11) Dec 17 2012 obj2asm is another one and I think it comes with dmd.
- js.mdnq (4/12) Dec 17 2012 With Visual D you can view the current disassembly. It is pretty
- maarten van damme (4/4) Dec 18 2012 I tried using objdump and a visual diff tool and I honestly can't
- bearophile (4/5) Dec 18 2012 Maybe there are some mistakes in your assumptions.
- maarten van damme (4/9) Dec 18 2012 My only assumption is that -version=runtime doesn't utilize ctfe and
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?
Dec 17 2012
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)();
Dec 17 2012
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
Dec 17 2012
Here is a trimmed down version : http://dpaste.dzfl.pl/11170641 thanks for the quick reply.
Dec 17 2012
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
Dec 17 2012
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
Dec 17 2012
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
Dec 17 2012
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
Dec 17 2012
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.
Dec 17 2012
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
Dec 17 2012
On 12/17/2012 02:47 PM, bearophile wrote:maarten van damme:obj2asm is another one and I think it comes with dmd. AliHow 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
Dec 17 2012
On Monday, 17 December 2012 at 22:47:33 UTC, bearophile wrote:maarten van damme:With Visual D you can view the current disassembly. It is pretty nice in seeing what dmd is doing behind the scenes along with the original D code inline.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
Dec 17 2012
I tried using objdump and a visual diff tool and I honestly can't find any differences (apart from the array creation at runtime). How can this be? http://dpaste.dzfl.pl/b96846ec
Dec 18 2012
maarten van damme:How can this be?Maybe there are some mistakes in your assumptions. Bye, bearophilwe
Dec 18 2012
My only assumption is that -version=runtime doesn't utilize ctfe and the other does. The rest is simple timing on both linux and windows and ctfe is way slower... 2012/12/18 bearophile <bearophileHUGS lycos.com>:maarten van damme:How can this be?Maybe there are some mistakes in your assumptions. Bye, bearophilwe
Dec 18 2012