|  |  | 
| |  | Posted by bearophile_hugs@eml.cc |  Permalink  Reply | 
 | 
| bearophile_hugs@eml.cc  
 
 
 | http://d.puremagic.com/issues/show_bug.cgi?id=7034
           Summary: Infinite foreach on array
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc
--- Comment #0 from bearophile_hugs@eml.cc 2011-11-29 19:11:45 PST ---
Maybe this issues is already present in Bugzilla.
I'd like this code to loop on all array 256 items once, initialize the array with all the bytes starting from the larger one, and then stop, but it goes into infinite loop:
void main() {
    ubyte[256] table;
    foreach (ubyte i, ref x; table)
        x = 255 - i;
}
But Jonathan M Davis suggests that 'i' should always be size_t if it's an
index:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=30863
This means Jonathan M Davis suggests to turn that code in a compilation error, so the right code become something like (the & is needed because currently D2 can't infer that  255-i  fits in one ubyte):
void main() {
    ubyte[256] table;
    foreach (i, ref x; table)
        x = (255 - i) & ubyte.max;
}
In any case, regardless of the solution that will be chosen (make it do the 'right' thing, or statically forbid that code requiring the index to be a size_t, or yet another solution), I suggest to not leave such trap in D2.
-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
 |