Literal types
Nick Treleaven
nick at geany.org
Sun Jul 6 19:25:04 UTC 2025
On Sunday, 6 July 2025 at 14:22:23 UTC, IchorDev wrote:
> 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.
I didn't say it did.
> ```d
> auto x = [1,2];
> ushort[2] y = cast(ushort[2])x;
> ```
You said integer literal type suffixes could be useful:
> One thing that could mitigate this is having explicit suffixes
> for char, byte, and short.
Instead I pointed you to array literal casts, which solves that
problem without repeating a suffix for every literal element of
the array literal, and it's a feature that already exists. Your
code is casting a non-literal, which the link I gave you
explicitly says means something different. That was not what I
suggested.
For the record, you can cast an array literal to a static array
type:
```d
ushort[2] y = cast(ushort[2]) [1,2];
assert(y == [1, 2]);
```
More information about the dip.ideas
mailing list