www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7034] New: Infinite foreach on array

reply d-bugmail puremagic.com writes:
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



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: -------
Nov 29 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7034




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: -------
Nov 30 2011
prev sibling parent d-bugmail puremagic.com writes:
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



*** 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: -------
Jan 16 2013