digitalmars.D.learn - Regarding foreach loop index ranges
- bearophile (20/20) Apr 21 2014 In this case I am not sure about bug reports, so I ask here.
- bearophile (19/25) Apr 21 2014 In this program the assignment to x1 is accepted, but the
- bearophile (3/3) Apr 21 2014 https://issues.dlang.org/show_bug.cgi?id=12611
- Steven Schveighoffer (9/24) Apr 21 2014 A third option is allow the code to compile, but throw an assert in
In this case I am not sure about bug reports, so I ask here. In this program the first loop doesn't compile, giving a nice error: test.d(3,5): Error: index type 'ubyte' cannot cover index range 0..300 If you comment out the first loop, the second compiles and runs, but it seems to go in an infinite loop: void main() { int[300] data; foreach (ubyte i, x; data) {} // Error foreach (ubyte i, x; data[]) {} // compiles } For the second loop one possible alternative behavour is to refuse a ubyte index and accept only a size_t index if it loops on a dynamic array. Another alternative is: the i variable can go from 0 to 255, then go up to the modulus of the remaining indexes, and then stop. What do you think? Bye, bearophile
Apr 21 2014
For the second loop one possible alternative behavour is to refuse a ubyte index and accept only a size_t index if it loops on a dynamic array. Another alternative is: the i variable can go from 0 to 255, then go up to the modulus of the remaining indexes, and then stop.In this program the assignment to x1 is accepted, but the assignments to x2 and x3 are refused: void main() { int[200] data1; ubyte x1 = data1.length; int[300] data2; ubyte x2 = data2.length; auto data3 = new int[300]; ubyte x3 = data3.length; } So I think the simplest solution is to refuse this code, because like the assignment of x3 it tries to assign a size_t value unknown at compile time to an ubyte: void main() { int[300] data; foreach (ubyte i, x; data[]) {} } Bye, bearophile
Apr 21 2014
https://issues.dlang.org/show_bug.cgi?id=12611 Bye, bearophile
Apr 21 2014
On Mon, 21 Apr 2014 07:49:03 -0400, bearophile <bearophileHUGS lycos.com> wrote:In this case I am not sure about bug reports, so I ask here. In this program the first loop doesn't compile, giving a nice error: test.d(3,5): Error: index type 'ubyte' cannot cover index range 0..300 If you comment out the first loop, the second compiles and runs, but it seems to go in an infinite loop: void main() { int[300] data; foreach (ubyte i, x; data) {} // Error foreach (ubyte i, x; data[]) {} // compiles } For the second loop one possible alternative behavour is to refuse a ubyte index and accept only a size_t index if it loops on a dynamic array. Another alternative is: the i variable can go from 0 to 255, then go up to the modulus of the remaining indexes, and then stop.A third option is allow the code to compile, but throw an assert in non-release mode when the foreach statement is encountered "ubyte cannot process all indexes of data." This would be the least code-breaking option that saves a bug (you don't know that the code can handle modulo indexes). Then you could deprecate to disallowing ubyte indexes. -Steve
Apr 21 2014