What is the type of loop index variables in D?

I started to study D, and I am having some problems with the examples given in Andrei Alexandrescu's book "Programming Language D". Some of the examples do not compile due to throws between the int and ulong types, one of which I will describe below.

I suspect that the problem is caused by the fact that I am using the 64-bit version of the compiler (Digital Mars 2.064.2 for 64-bit Ununtu), and the examples in the book were tested with the 32-bit compiler.

The following code:

#!/usr/bin/rdmd
import std.stdio;
void main(){
    int[] arr = new int[10];
    foreach(i, ref a; arr)
        a = i+1;
    writeln(arr);
}

unable to execute the following compiler error

bcumming@arapiles:chapter1 > ./arrays.d 
./arrays.d(9): Error: cannot implicitly convert expression (i + 1LU) of type ulong to int
Failed: 'dmd' '-v' '-o-' './arrays.d' '-I.'

I can fix this by explicitly declaring a variable of type int:

foreach(int i, ref a; arr)
    a = i+1;

What are the rules that determine what type of default loop index will be in D? Is it because I'm using a 64-bit compiler?

+4
2

, array.length: size_t. 32 64 .

+5

D size_t. .

+5

All Articles