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

IchorDev zxinsworld at gmail.com
Tue Jan 21 21:33:24 UTC 2025


On Monday, 20 January 2025 at 23:09:24 UTC, Walter Bright wrote:
> I haven't really thought about that. But this also works and is 
> simpler and easier to read:
>
> ```
> void[] foo = cast(void[])((&bar)[0 .. 1]);
> ```

Doesn’t this incur a runtime bounds check? Also does this work 
with non-void slices?

> That wouldn't be good because it would stomp on whatever is 
> next in memory.

No, it would allocate new stack memory or use a new register. 
You’ll assign the expression to something anyway, so it’s like if 
you declared a default initialised variable and partially 
overwrote it. I’ve actually needed this pattern quite a lot when 
interacting with C and often I just have to cast to `ubyte[]` and 
write all the lengths manually. Looks ugly.


More information about the dip.development mailing list