[Issue 16357] cast(T[])[x] casts x to T instead of [x] to T[]

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Oct 4 22:19:10 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=16357

Steven Schveighoffer <schveiguy at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |spec
                 CC|                            |schveiguy at gmail.com

--- Comment #3 from Steven Schveighoffer <schveiguy at gmail.com> ---
This is technically in the spec:
https://dlang.org/spec/expression.html#cast_array_literal

Quoting:
```
When array literals are cast to another array type, each element of the array
is cast to the new element type.
```

But I don't know if the spec (which only shows casting arrays with elements of
value 1) or this issue (which has elements of value 0) explores the destructive
nature of the current implementation:

```d
void main()
{
    uint x = 0x12345;
    ubyte[] data = cast(ubyte[])[x];
    assert(data == [0x45]); // we lost the extra bytes!
}
```

My thought is that this should either be an error, or a warning in the spec is
warranted.

--


More information about the Digitalmars-d-bugs mailing list