Literal types
IchorDev
zxinsworld at gmail.com
Sun Jul 6 14:22:23 UTC 2025
On Saturday, 5 July 2025 at 15:48:43 UTC, Nick Treleaven wrote:
> On Saturday, 5 July 2025 at 10:50:13 UTC, Nick Treleaven wrote:
>> You can write `cast(ubyte) [1, 2]`:
>
> I meant `cast(ubyte[]) [1, 2]`.
>
>> https://dlang.org/spec/expression.html#cast_array_literal
It doesn’t work with static arrays so it doesn’t even solve one
of Quinn’s first examples.
```d
auto x = [1,2];
ushort[2] y = cast(ushort[2])x;
```
The unspoken problem here is integer arithmetic: putting explicit
casts on expressions SUCKS. There’s no nicer way to say it. The
code ugliness is over 100%. If you haven’t had to add
`cast(ushort)(exp)` to hundreds of expressions before, then count
yourself lucky because it defaces pretty code into a hideous
mess. I still fail to see Walter’s reasoning as to how
compatibility with C’s integer promotion is more important than
compatibility with its function pointer syntax, truncating
implicit casts, etc. Integer promotion doesn’t even prevent
overflows for int/long types, similar to how C’s `const` is
completely useless. So, a better system would just ask the
programmer to be specific about how they want/expect each integer
to handle overflow (see std.checkedint… which doesn’t even work
properly with bytes/shorts because of integer promotion).
Or: just add a flag to disable integer promotion. ;)
More information about the dip.ideas
mailing list