Thread overview
[Issue 7034] New: Infinite foreach on array
Jan 17, 2013
yebblies
November 30, 2011
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: -------
December 01, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7034



--- Comment #1 from bearophile_hugs@eml.cc 2011-11-30 17:00:40 PST ---
Partially related:

http://www.digitalmars.com/d/archives/digitalmars/D/learn/Foreach_with_byte_problems_24997.html

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7034


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies@gmail.com
         Resolution|                            |DUPLICATE


--- Comment #2 from yebblies <yebblies@gmail.com> 2013-01-17 13:54:13 EST ---
*** This issue has been marked as a duplicate of issue 5725 ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------