Range Error

Imperatorn johan_forsberg_86 at hotmail.com
Mon Apr 12 18:13:38 UTC 2021


On Monday, 12 April 2021 at 18:01:02 UTC, kdevel wrote:
> On Sunday, 11 April 2021 at 20:41:35 UTC, Bastiaan Veelo wrote:
> [...]
>>> [...]
>>
>> Yes, there is a `7` where there should be an `i` on this line:
>> ```d
>>    for(int i=7;7>=0;i--)
>> ```
>> This will go on forever, so you get a range error as soon as 
>> `i < 0`.
>
> Can't these kinds of errors be eliminated by using foreach 
> loops?:
>
> ```d
>     Square[8][8] square;
>     foreach (int i, ref v; square)
>     {
>         foreach(int j, ref w; v)
>         {
>             if((isEven(i)&&isEven(j))^(!isEven(i)&&!isEven(j)))
>                 w = new Square(Color.none, sColor.white);
>             else 
> if((!isEven(i)&&isEven(j))^(isEven(i)&&!isEven(j)))
>                 w = new Square(Color.none,sColor.black);
>             else
>                 assert(false);
>
>         }
>     }
> ```

Yup


More information about the Digitalmars-d-learn mailing list