Force lvalue (`cast(ref)`)

Atila Neves atila.neves at gmail.com
Mon Feb 17 09:05:55 UTC 2025


On Thursday, 13 February 2025 at 22:41:21 UTC, Quirin Schroll 
wrote:
> On Wednesday, 12 February 2025 at 09:36:15 UTC, Atila Neves 
> wrote:
>> [...]
>
> 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.

I think lack of imagination when it comes to a use case is 
definitely an argument in favour of not adding a feature.


More information about the dip.ideas mailing list