First Draft: cast(ref T)... as shorthand for *cast(T*)&...

IchorDev zxinsworld at gmail.com
Wed Jan 22 08:37:49 UTC 2025


On Wednesday, 22 January 2025 at 01:01:58 UTC, Walter Bright 
wrote:
> It is a non-void slice!

I meant doing this:
`T[] foo = cast(T[])((&bar)[0 .. 1]);`
Where bar is (e.g.) `int[]`.

> Reading past the end of a memory object could produce a seg 
> fault. Writing is a near certain disaster.

I mean that a reinterpret cast from `uint` to ` long` would lower 
to this:
```d
uint x;
long y;
(cast(ubyte[8])long)[0..4] = (cast(ubyte[4])x)[];
```

Also you missed my alternative syntax suggestion:

> What about `cast(in T)` instead? As in ‘re-INterpret’. `in` 
> also doesn’t make sense for local variables, and already has 
> multiple meanings, so it’s easier to see that the keyword has a 
> different meaning for `cast(in T)`.

I’m against this change but only because the proposed syntax is 
adding the first ever exception to `ref`’s usual meaning. 
`cast(ref T)` should be reserved for a feature consistent with 
the existing meaning of `ref`.


More information about the dip.development mailing list