www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - variable x cannot be read at compile time (ctfe)

reply maarten van damme <maartenvd1994 gmail.com> writes:
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
next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
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
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent reply maarten van damme <maartenvd1994 gmail.com> writes:
Here is a trimmed down version : http://dpaste.dzfl.pl/11170641

thanks for the quick reply.
Dec 17 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
 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
parent reply maarten van damme <maartenvd1994 gmail.com> writes:
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
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent reply maarten van damme <maartenvd1994 gmail.com> writes:
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
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 12/17/2012 02:47 PM, bearophile wrote:
 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
obj2asm is another one and I think it comes with dmd. Ali
Dec 17 2012
prev sibling parent reply "js.mdnq" <js_adddot+mdng gmail.com> writes:
On Monday, 17 December 2012 at 22:47:33 UTC, bearophile wrote:
 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
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.
Dec 17 2012
parent reply maarten van damme <maartenvd1994 gmail.com> writes:
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
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
maarten van damme:

 How can this be?
Maybe there are some mistakes in your assumptions. Bye, bearophilwe
Dec 18 2012
parent maarten van damme <maartenvd1994 gmail.com> writes:
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