Re: How many people here use stackoverflow.com?
On Sun, 24 May 2009 06:45:51 +1200, Bill Baxter wbax...@gmail.com wrote: Yeh, denied because search is really hard. So they cant just insert an if statement on the line before to check for exact match before trying the search?
Template limits in D2
I am trying to create a non-dynamic array at compile time, so I have written this test code: int sumSqrt(int n) { int result = 0; while (n) { int digit = n % 10; n /= 10; result += digit * digit; } return result; } template GenSquares(int n) { static if (n 0) const int[] GenSquares = []; else const int[] GenSquares = GenSquares!(n-1) ~ [sumSqrt(n)]; } void main() { const int CHUNK = 1000; static const auto squares = cast(int[CHUNK])GenSquares!(CHUNK-1); } That code works with D1. But with D2 it gives: templates_test.d(15): Error: template instance templates_test.GenSquares!(499) recursive expansion Do you know why D2 allows less templates? The second problem is that compile-time functions are nicer, so I'd like to not use templates when possible. But the following code doesn't work at compile time, can you tell me why? (I have had to use a not nice temporary struct to return the static array) struct S(int N) { int[N] a; } S!(N) genSquares(int N)() { S!(N) s; for (int i = 0; i N; i++) { int n = i; int m = 0; while (n) { int digit = n % 10; n /= 10; m += digit * digit; } s.a[i] = m; } return s; } void main() { const int CHUNK = 1000; static const auto squares = genSquares!(CHUNK)().a; } Bye and thank you, bearophile
Re: Template limits in D2
bearophile wrote: ... The second problem is that compile-time functions are nicer, so I'd like to not use templates when possible. But the following code doesn't work at compile time, can you tell me why? (I have had to use a not nice temporary struct to return the static array) struct S(int N) { int[N] a; } S!(N) genSquares(int N)() { S!(N) s; for (int i = 0; i N; i++) { int n = i; int m = 0; while (n) { int digit = n % 10; n /= 10; m += digit * digit; } s.a[i] = m; } return s; } void main() { const int CHUNK = 1000; static const auto squares = genSquares!(CHUNK)().a; } Bye and thank you, bearophile I'm not sure why, this code does work: int[] genSquares(int N)() { int[] a; for (int i = 0; i N; i++) { int n = i; int m = 0; while (n) { int digit = n % 10; n /= 10; m += digit * digit; } a~=m; } return a; } void main() { enum int CHUNK = 1000; enum int[CHUNK] squares = genSquares!(CHUNK)(); } However, it fails as soon as I try do use indexing or set the length of an array. I thought these operations were supposed to be legal, perhaps it is a bug.
Re: Template limits in D2
Lutger escribió: bearophile wrote: ... The second problem is that compile-time functions are nicer, so I'd like to not use templates when possible. But the following code doesn't work at compile time, can you tell me why? (I have had to use a not nice temporary struct to return the static array) struct S(int N) { int[N] a; } S!(N) genSquares(int N)() { S!(N) s; for (int i = 0; i N; i++) { int n = i; int m = 0; while (n) { int digit = n % 10; n /= 10; m += digit * digit; } s.a[i] = m; } return s; } void main() { const int CHUNK = 1000; static const auto squares = genSquares!(CHUNK)().a; } Bye and thank you, bearophile I'm not sure why, this code does work: int[] genSquares(int N)() { int[] a; for (int i = 0; i N; i++) { int n = i; int m = 0; while (n) { int digit = n % 10; n /= 10; m += digit * digit; } a~=m; } return a; } void main() { enum int CHUNK = 1000; enum int[CHUNK] squares = genSquares!(CHUNK)(); } However, it fails as soon as I try do use indexing or set the length of an array. I thought these operations were supposed to be legal, perhaps it is a bug. I just debugged it with Descent, and it seems the static array is the problem. It can't be interpreted. But doing this works: --- struct S(int N) { int[] a; } ... s.a ~= m; --- I think static arrays don't work in compile-time functions. Like, you can't return one from a function, so that might be the problem, I don't know.
Re: Template limits in D2
Ary Borenszweig escribió: Lutger escribió: bearophile wrote: ... The second problem is that compile-time functions are nicer, so I'd like to not use templates when possible. But the following code doesn't work at compile time, can you tell me why? (I have had to use a not nice temporary struct to return the static array) struct S(int N) { int[N] a; } S!(N) genSquares(int N)() { S!(N) s; for (int i = 0; i N; i++) { int n = i; int m = 0; while (n) { int digit = n % 10; n /= 10; m += digit * digit; } s.a[i] = m; } return s; } void main() { const int CHUNK = 1000; static const auto squares = genSquares!(CHUNK)().a; } Bye and thank you, bearophile I'm not sure why, this code does work: int[] genSquares(int N)() { int[] a; for (int i = 0; i N; i++) { int n = i; int m = 0; while (n) { int digit = n % 10; n /= 10; m += digit * digit; } a~=m; } return a; } void main() { enum int CHUNK = 1000; enum int[CHUNK] squares = genSquares!(CHUNK)(); } However, it fails as soon as I try do use indexing or set the length of an array. I thought these operations were supposed to be legal, perhaps it is a bug. I just debugged it with Descent, and it seems the static array is the problem. It can't be interpreted. But doing this works: --- struct S(int N) { int[] a; } ... s.a ~= m; --- I think static arrays don't work in compile-time functions. Like, you can't return one from a function, so that might be the problem, I don't know. BTW, I had to debug inside Descent's code to find this. If I debug it using the debugger I'm programming, I can see it stops the execution right at the s.a[i] = m; line, without saying why (DMD doesn't say why). It's not much, but I think it's better than Can't evaluate at compile-time, and might give you more clues about it. :-)
Re: Template limits in D2
Hello Ary, BTW, I had to debug inside Descent's code to find this. If I debug it using the debugger I'm programming, I can see it stops the execution right at the s.a[i] = m; line, without saying why (DMD doesn't say why). It's not much, but I think it's better than Can't evaluate at compile-time, and might give you more clues about it. :-) DMD feature request?
Re: Template limits in D2
Ary Borenszweig wrote: ... BTW, I had to debug inside Descent's code to find this. If I debug it using the debugger I'm programming, I can see it stops the execution right at the s.a[i] = m; line, without saying why (DMD doesn't say why). It's not much, but I think it's better than Can't evaluate at compile-time, and might give you more clues about it. :-) yes it's this: s.a[i] = m, it doesn't work with dynamic arrays either.
Re: Applying const to an object but not the container (D 2.0)
Burton Radons wrote: I'm writing an XML class. There are two tests for this class, isAncestorOf and isDescendantOf, that are implemented in terms of one another. They're both const, and look like this: class Node { Node parentNode; /// ... /// Return whether this is an ancestor of the other node. A node is not an ancestor of itself, or of null. bool isAncestorOf (Node node) const { while (node) if ((node = node.parentNode) is this) return true; return false; } /// Return whether one of the parents of this node is the other. A node is not a descendant of itself, or of null. bool isDescendantOf (Node node) const { return node ? node.isAncestorOf (this) : false; } } The compiler doesn't like this, saying of the isDescendantOf essentially that this is const, but is being passed as mutable. However, if I make the argument const, then the assignment in the loop won't work because unlike const (Struct) *, const (Class) applies both to the object and the container. Is there a way around this aside from recursion? std.typecons.rebindable a pointer with a nice wrapper basically. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Why do I get stack overflow?
When I compile this code I get stack overflow printed in the console. Anyone know why? --- int fact(int X)() { if(X == 0) { return 1; } else { int temp = fact!(X - 1)(); return X * temp; } } const someVar = fact!(0)(); ---
Re: Why do I get stack overflow?
Ary Borenszweig wrote: When I compile this code I get stack overflow printed in the console. Anyone know why? --- int fact(int X)() { if(X == 0) { return 1; } else { int temp = fact!(X - 1)(); return X * temp; } } const someVar = fact!(0)(); --- Like Moritz said. You need to use static if there rather than if.
Re: Why do I get stack overflow?
On Sun, 24 May 2009 20:49:53 -0300, Ary Borenszweig wrote: When I compile this code I get stack overflow printed in the console. Anyone know why? --- int fact(int X)() { if(X == 0) { return 1; } else { int temp = fact!(X - 1)(); return X * temp; } } const someVar = fact!(0)(); --- Because you generate fact!(-1)(), fact!(-2)() and so on at compile time. You recursive template doesn't terminate.
Re: Why do I get stack overflow?
Moritz Warning escribió: On Sun, 24 May 2009 20:49:53 -0300, Ary Borenszweig wrote: When I compile this code I get stack overflow printed in the console. Anyone know why? --- int fact(int X)() { if(X == 0) { return 1; } else { int temp = fact!(X - 1)(); return X * temp; } } const someVar = fact!(0)(); --- Because you generate fact!(-1)(), fact!(-2)() and so on at compile time. You recursive template doesn't terminate. Thanks. Later in my head I instantiated the template and noticed the problem.