D design problem on platforms with <32 bit pointer width

Dom DiSc dominikus at scherkl.de
Sat Aug 19 21:47:33 UTC 2023


On Saturday, 19 August 2023 at 10:55:45 UTC, Dukc wrote:
> ```d
> auto x = 40;
> // 1600 now, 64 with your rules
> x *= x;
> ```
>
> Inference happens at initialisation of the variable.

Sorry, but if you say nothing about which type you want, you may 
always be surprised unless you know the inference rules.
You could have written
```d
int x = 40;
```
which incidentally is even one character shorter. And then you 
have the guarantee that you have 31 bit (+sign) for your 
calculations - no matter on which hardware you're working.
In your example you expect the C rules. But if the program runs 
on a 8-bit system, what you get will be a (signed) byte! So the 
result of your square may be even negative, breaking a whole lot 
of further assumptions on the result.



More information about the Digitalmars-d mailing list