Summary: Infinite foreach on array
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD

--- Comment #0 from 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

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:
------- You are receiving this mail because: -------

Reply via email to