digitalmars.D.learn - Range Error
- Ruby The Roobster (93/93) Apr 11 2021 So, here is the full code:
- Ruby The Roobster (3/7) Apr 11 2021 Nevermind. I messed up what line it was. It was actually this
- martinm (3/11) Apr 15 2021 This is maybe interesting for you:
- Bastiaan Veelo (8/9) Apr 11 2021 Yes, there is a `7` where there should be an `i` on this line:
- Steven Schveighoffer (19/27) Apr 11 2021 Also this code is wrong:
- Ruby The Roobster (3/15) Apr 12 2021 I fixed the code now. It works. Thanks for your help.
- Ruby The Roobster (2/12) Apr 12 2021 I have fixed this.
- kdevel (19/26) Apr 12 2021 Can't these kinds of errors be eliminated by using foreach loops?:
- Imperatorn (2/30) Apr 12 2021 Yup
- kdevel (56/57) Apr 12 2021 D can be so much fun!
- Imperatorn (2/9) Apr 12 2021 Of course :D
So, here is the full code: ```d enum Color {none=0,red=1,black=2}; enum sColor {black=0,white=1}; class Square { public: this(Color color, sColor Scolor) { this.color = color; this.Scolor = Scolor; } Color color; sColor Scolor; } import std.stdio; void cPrintboard(Square[8][8] square) { for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { final switch(square[i][j].color) { case Color.none: write(" n "); break; case Color.red: write(" r "); break; case Color.black: write(" b "); break; } } writeln(""); } writeln(""); } void sPrintboard(Square[8][8] square) { for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { final switch(square[i][j].Scolor) { case sColor.white: write(" w "); break; case sColor.black: write(" b "); break; } } writeln(""); } writeln(""); } bool isEven(int a) { return a&1; } void main() { Square[8][8] square; for(int i=7;7>=0;i--) { for(int j=7;j>=0;j--) { if((isEven(i)&&isEven(j))^(!isEven(i)&&!isEven(j))) square[i][j] = new Square(Color.none, sColor.white); else if((!isEven(i)&&isEven(j))^(isEven(i)&&!isEven(j))) square[i][j] = new Square(Color.none,sColor.black); else assert(false); } } for(int i=2;i>=0;i--) { for(int j=7;i>=0;j--) { if((!isEven(i)&&isEven(j))^(isEven(i)&&!isEven(j))) square[i][j].color = Color.red; //The program breaks here, with a range violation message else { } } } cPrintboard(square); sPrintboard(square); } ``` What am I doing wrong here? Is it the 'for' loop?
Apr 11 2021
On Sunday, 11 April 2021 at 19:45:30 UTC, Ruby The Roobster wrote:What am I doing wrong here? Is it the 'for' loop?Nevermind. I messed up what line it was. It was actually this line:```d square[i][j] = new Square(Color.none, sColor.white); ```
Apr 11 2021
On Sunday, 11 April 2021 at 19:48:42 UTC, Ruby The Roobster wrote:On Sunday, 11 April 2021 at 19:45:30 UTC, Ruby The Roobster wrote:This is maybe interesting for you: https://wiki.dlang.org/Component_programming_with_rangesWhat am I doing wrong here? Is it the 'for' loop?Nevermind. I messed up what line it was. It was actually this line:```d square[i][j] = new Square(Color.none, sColor.white); ```
Apr 15 2021
On Sunday, 11 April 2021 at 19:45:30 UTC, Ruby The Roobster wrote:What am I doing wrong here? Is it the 'for' loop?Yes, there is a `7` where there should be an `i` on this line: ```d for(int i=7;7>=0;i--) ``` This will go on forever, so you get a range error as soon as `i < 0`. —Bastiaan.
Apr 11 2021
On 4/11/21 4:41 PM, Bastiaan Veelo wrote:On Sunday, 11 April 2021 at 19:45:30 UTC, Ruby The Roobster wrote:Also this code is wrong: ```d for(int i=2;i>=0;i--) { for(int j=7;i>=0;j--) ``` For that j loop, i will always be 2, so it will not terminate until the range error happens. Should probably be: for(int j=7; j >= 0; j--) I would also suggest for more readable code, use foreach + iota or foreach_reverse: ```d foreach_reverse(i; 0 .. 3) // or foreach(i; iota(2, -1, -1)) { foreach_reverse(j; 0 .. 8) ``` -SteveWhat am I doing wrong here? Is it the 'for' loop?Yes, there is a `7` where there should be an `i` on this line: ```d for(int i=7;7>=0;i--) ``` This will go on forever, so you get a range error as soon as `i < 0`.
Apr 11 2021
On Monday, 12 April 2021 at 01:22:12 UTC, Steven Schveighoffer wrote:On 4/11/21 4:41 PM, Bastiaan Veelo wrote:I fixed the code now. It works. Thanks for your help.On Sunday, 11 April 2021 at 19:45:30 UTC, Ruby The Roobster wrote:...What am I doing wrong here? Is it the 'for' loop?Yes, there is a `7` where there should be an `i` on this line: ```d for(int i=7;7>=0;i--) ``` This will go on forever, so you get a range error as soon as `i < 0`.
Apr 12 2021
On Sunday, 11 April 2021 at 20:41:35 UTC, Bastiaan Veelo wrote:On Sunday, 11 April 2021 at 19:45:30 UTC, Ruby The Roobster wrote:I have fixed this.What am I doing wrong here? Is it the 'for' loop?Yes, there is a `7` where there should be an `i` on this line: ```d for(int i=7;7>=0;i--) ``` This will go on forever, so you get a range error as soon as `i < 0`. —Bastiaan.
Apr 12 2021
On Sunday, 11 April 2021 at 20:41:35 UTC, Bastiaan Veelo wrote: [...]Can't these kinds of errors be eliminated by using foreach loops?: ```d Square[8][8] square; foreach (int i, ref v; square) { foreach(int j, ref w; v) { if((isEven(i)&&isEven(j))^(!isEven(i)&&!isEven(j))) w = new Square(Color.none, sColor.white); else if((!isEven(i)&&isEven(j))^(isEven(i)&&!isEven(j))) w = new Square(Color.none,sColor.black); else assert(false); } } ```What am I doing wrong here? Is it the 'for' loop?Yes, there is a `7` where there should be an `i` on this line: ```d for(int i=7;7>=0;i--) ``` This will go on forever, so you get a range error as soon as `i < 0`.
Apr 12 2021
On Monday, 12 April 2021 at 18:01:02 UTC, kdevel wrote:On Sunday, 11 April 2021 at 20:41:35 UTC, Bastiaan Veelo wrote: [...]YupCan't these kinds of errors be eliminated by using foreach loops?: ```d Square[8][8] square; foreach (int i, ref v; square) { foreach(int j, ref w; v) { if((isEven(i)&&isEven(j))^(!isEven(i)&&!isEven(j))) w = new Square(Color.none, sColor.white); else if((!isEven(i)&&isEven(j))^(isEven(i)&&!isEven(j))) w = new Square(Color.none,sColor.black); else assert(false); } } ```[...]Yes, there is a `7` where there should be an `i` on this line: ```d for(int i=7;7>=0;i--) ``` This will go on forever, so you get a range error as soon as `i < 0`.
Apr 12 2021
On Monday, 12 April 2021 at 18:13:38 UTC, Imperatorn wrote: [...]YupD can be so much fun! ```d import std.stdio; enum Color {none = " n ", red = " r ", black = " b "}; enum sColor {black= " b ", white= " w "}; class Square { public: this(Color color, sColor Scolor) { this.color = color; this.Scolor = Scolor; } Color color; sColor Scolor; } void Printboard (alias Q) (Square[8][8] square) { foreach (int i, ref v; square) { foreach(int j, ref w; v) { string s = Q (w); write (s); } writeln; } writeln; } bool isEven(int a) { return a&1; } void main() { Square[8][8] square; foreach (int i, ref v; square) foreach(int j, ref w; v) { auto color = (i + j).isEven ? sColor.black : sColor.white; w = new Square(Color.none, color); } foreach (int i, ref v; square) { if (i > 2) continue; foreach (int j, ref w; v) w.color = (i + j).isEven ? Color.red : w.color; } Printboard!(w => w.color) (square); Printboard!(w => w.Scolor) (square); } ```
Apr 12 2021
On Monday, 12 April 2021 at 19:19:12 UTC, kdevel wrote:On Monday, 12 April 2021 at 18:13:38 UTC, Imperatorn wrote: [...]Of course :D[...]D can be so much fun! ```d import std.stdio; [...]
Apr 12 2021