Bring back foreach int indexes

deadalnix deadalnix at gmail.com
Wed Nov 29 16:56:54 UTC 2023


On Wednesday, 29 November 2023 at 14:56:50 UTC, Steven 
Schveighoffer wrote:
> I don’t know how many times I get caught with size_t indexes 
> but I want them to be int or uint. It’s especially painful in 
> my class that I’m teaching where I don’t want to yet explain 
> why int doesn’t work there and have to introduce casting or use 
> to!int. All for the possibility that I have an array larger 
> than 2 billion elements.
>
> I am forgetting why we removed this in the first place.
>
> Can we have the compiler insert an assert at the loop start 
> that the bounds are in range when you use a smaller int type? 
> Clearly the common case is that the array is small enough for 
> int indexes.
>
> -Steve

It cannot work in the general case, slice's size is a size_t .

But if the compiler can prove it fits in 32bits, then there 
should be no problem. VRP could do that.


More information about the Digitalmars-d mailing list