D design problem on platforms with <32 bit pointer width

Dukc ajieskola at gmail.com
Sat Aug 19 10:55:45 UTC 2023


On Saturday, 19 August 2023 at 10:43:59 UTC, Dom DiSc wrote:
> I hate those stupid promotion rules.
> Every literal should be of the smallest type that can represent 
> it
> (e.g. 0..255 should be ubyte, 256..65535 should be ushort, 
> etc., -1..-127 should be byte, -128..-32767 should be short, 
> ...) and can be promoted to whatever is needed implicitly.

It's not quite that simple. Consider:

```d
auto x = 40;
// 1600 now, 64 with your rules
x *= x;
```

Inference happens at initialisation of the variable. In this 
case, it's `ubyte` because nothing bigger is needed to hold the 
initialisation value. Inference can't detect what is needed 
later, that is why int literals default to 32 bits.


More information about the Digitalmars-d mailing list