D Automatic type errors (64-bit problem?)

A little about myself: I'm on Mac OSX Lion (64 bit, obviously), and I'm a long time Java developer interested in learning about D.

I took a copy of the D programming language, and I'm a little confused with a few things. Firstly, if I try something like the following (directly 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 an expression (i + 1LU) of type ulong to int

This is fixed by changing i to int i .

I think this is due to the fact that the automatic type for numbers on a 64-bit platform is ulong , so type inference does not really work.

Now I have the following problem:

 bool binarySearch(T)(T[] input, T value) { // ... int i = input.length / 2; // ... } 

This returns the following compilation error:

Error: cannot implicitly convert an expression (input.length / 2LU) of type ulong to int

Casting fixes this, but I would prefer. I also get other stupid errors related to getting long values โ€‹โ€‹from calculations and then unable to use them to index into arrays. D 64-bit support still not sniffed? What can I do to avoid future troubles while learning D? The explicit use of translations and types everywhere seems to be the opposite of what attracted me to the language in the first place ...

+6
source share
2 answers

I think this is due to the fact that the automatic type for numbers on a 64-bit platform is ulong, so type inference does not work.

Right. If this is an error in TDPL, you should probably send errors. The foreach index variable is usually of type size_t .

Error: cannot implicitly convert an expression (input.length / 2LU) of type ulong to int

Change your code to:

 size_t i = input.length / 2; 

Or even better, so you donโ€™t have to think about it:

 auto i = input.length / 2; 

What you see is, in fact, full-blown support for 64-bit arrays. size_t allows its own integer type and is used to index the array and the length of the array.

+10
source

in future

to someone

 int[] months = new int[12]; foreach (int i, ref month; months) { month = i + 1; } 
0
source

Source: https://habr.com/ru/post/910876/


All Articles