A little background: I am on Mac OSX Lion (64 bit, obviously) and I am a long time Java developer interested in learning D.
I picked up a copy of The D Programming Language and I am confused on a few things. First, if I try something like the following (straight from the book):
int[] months = new int[12];
foreach (i, ref month; months) {
month = i + 1;
}
I get the following error:
Error: cannot implicitly convert expression (i + 1LU) of type ulong to int
This is fixed by changing i to int i.
I think this is because the automatic type for numbers on a 64 bit platform is ulong and so type inference doesn't really work out.
Now I have the following problem:
bool binarySearch(T)(T[] input, T value) {
// ...
int i = input.length / 2;
// ...
}
That returns the following compile error:
Error: cannot implicitly convert expression (input.length / 2LU) of type ulong to int
Casting fixes this, but I'd rather not. I also get other silly errors relating to getting long values from calculations and then not being able to use them for indexing into arrays. Is D's 64 bit support just not up to snuff yet? What can I do to avoid trouble in the future while learning D? Having to explicitly use casts and types everywhere seems to be the opposite of what attracted me to the language in the first place...
size_twhen indexing an array. This makes your code much more correct (and platform-independent), though of course, D handles this typing for you. (In both C and D,size_tis almost always automatically aliased to either an unsigned 32-bit integer or an unsigned 64-bit, which is why you're seeing one of those names instead ofsize_t.) - user541686