www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Regarding foreach loop index ranges

reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
 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
parent "bearophile" <bearophileHUGS lycos.com> writes:
https://issues.dlang.org/show_bug.cgi?id=12611

Bye,
bearophile
Apr 21 2014
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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