Force lvalue (`cast(ref)`)

Quirin Schroll qs.il.paperinik at gmail.com
Thu Feb 13 22:41:21 UTC 2025


On Wednesday, 12 February 2025 at 09:36:15 UTC, Atila Neves wrote:
> On Wednesday, 22 January 2025 at 08:58:56 UTC, IchorDev wrote:
>> [...]
>
> Doesn't -preview=rvaluerefparam do this already? Or am I 
> misunderstanding?

Yes, it does implicitly if it has to.
```d
// requires -preview=rvaluerefparam
void f(ref int x) { }

f(10); // materializes a local variable, assigns 10, passes it to 
`f`
```

```d
// with and without -preview=rvaluerefparam
void f(ref int x) { }
void f(int x) { }

f(10); // simply calls `f(int)`
```

I guess what the `cast(ref)` could do is this:
```d
// with and without -preview=rvaluerefparam
void f(ref int x) { }
void f(int x) { }

f(cast(ref)10); // NEW: forces materializing a local variable, 
calls `f(ref int)`
```

I can’t imagine a use case, but lack of imagination is hardly an 
argument.


More information about the dip.ideas mailing list