Thread overview
Force lvalue (`cast(ref)`)
Jan 22
IchorDev
Jan 22
Juraj
3 hours ago
Atila Neves
January 22

Sometimes you want an rvalue to bind to ref, but the compiler can’t be sure the reference isn’t escaped. But if you know that the reference won’t be escaped, or you don’t care if your code is safe at all, you can use cast(ref) to forcibly turn an rvalue into an lvalue:

ref x = cast(ref)10;
x++; //10 exists for the duration of this function. If this function returns x by reference, it is undefined behaviour.
auto y = returnPassedRef(cast(ref)24);
y++; //24 exists for the duration of the function it was created in, so `returnPassedRef` safely returned it. If this function returns the reference however, it is undefined behaviour.

The cast is @system . If the lvalue reference is escaped, it is undefined behaviour.
Passing a newly made lvalue into return ref parameters should probably issue a warning.

This feature is useful in writing low-level code where safety is manually verified, especially when interfacing with C++ libraries.

Possible alternative: the hideous __lvalue. However, __rvalue is dissimilar in that it is not @system and does not produce undefined behaviour, whereas casts are often @system , so perhaps the dissimilar syntax is a good reflection of the differences between the two.

January 22
Why can we not make this "just work" without extra syntax?

I.e.

```d
void func(ref int);
func(2);
```

January 22

On Wednesday, 22 January 2025 at 09:02:14 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

Why can we not make this "just work" without extra syntax?

I.e.

void func(ref int);
func(2);

Because there is the curse of preview switches ...
https://github.com/dlang/dmd/pull/17068

3 hours ago

On Wednesday, 22 January 2025 at 08:58:56 UTC, IchorDev wrote:

>

[...]

Doesn't -preview=rvaluerefparam do this already? Or am I misunderstanding?