foreach counter now must be size_t ?

Walter Bright newshound2 at digitalmars.com
Tue Feb 5 04:53:00 UTC 2019


On 2/4/2019 7:15 PM, Timon Gehr wrote:
> I wish it wasn't. It seems this doesn't do anything useful. If I'm going through 
> the trouble of explicitly specifying the counter type to be what I need, why 
> annoy me with this error message?

It is the same thing as:

     long l;
     uint i = l; // error

D doesn't allow implicit narrowing conversions that lose information.

If the foreach was written like this:

     void foo(int[3] arr) {
       foreach (uint i, ref elem; arr) { }
     }

I.e. where the array bounds are known at compile time, then it works, because:

     enum long l = 3;
     uint i = l;

also works.

As for "who would ever have an array that big":

1. It's irrelevant, as the semantics of the language have to be sound across the 
uses it supports.

2. I noticed with Windows MovieMaker that it produces corrupt output files if 
the movie is bigger than 4Gb. Evidently a 32 bit counter isn't enough.


More information about the Digitalmars-d mailing list