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

Juraj junk at vec4.xyz
Mon Jan 20 10:07:42 UTC 2025


On Saturday, 18 January 2025 at 07:31:40 UTC, Walter Bright wrote:
> Proposed by Manu https://github.com/dlang/dmd/issues/20644
>
> PR: https://github.com/dlang/dmd/pull/20728


This is how an unaware reader will parse the code:
```d
ubyte val = 123;
auto x = cast(const long) val;     // x is const
auto y = cast(immutable long) val; // y is immutable
auto z = cast(shared long) val;    // z is shared

auto w = cast(ref long) val;       // w is ref
```

We now have local `ref`, and that makes this syntax a cognitive 
burden.

I know, that the type is `const(long)` and there is no 
`ref(long)` so this is technically correct, still, it will 
confuse users for a very little benefit.
Reinterpret cast is not a "commonly used patterns" it is a 
necessary evil, one can always make a local helper for it, if 
importing a module is a compile time issue.

Another example:
```d
auto c0 = cast(scope long) val;   // Error
auto c1 = cast(ref long) val;     // Compiles, but confusing.
```

I am a sucker for syntax sugar, but this one, imho, sucks.

Juraj



More information about the dip.development mailing list