Thread overview
Why can't rvalues be passed to a 'ref' parameter?
Dec 11, 2022
thebluepandabear
Dec 11, 2022
zjh
Dec 11, 2022
Ali Çehreli
Dec 12, 2022
zjh
Dec 11, 2022
ryuukk_
December 11, 2022

Hello,

I am not really understanding why rvalues cannot be passed to a 'ref' parameter, the explanation in the book about D I am reading was not clear:

"The main reason for this limitation is the fact that a function taking a ref
parameter can hold on to that reference for later use, at a time when the rvalue
would not be available."

I didn't really understand what Ali meant by this statement, any help would be appreciated so this can be clarified.

Regards,
thebluepandabear

December 11, 2022

On Sunday, 11 December 2022 at 04:36:45 UTC, thebluepandabear wrote:

>

"The main reason for this limitation is the fact that a function taking a ref
parameter can hold on to that reference for later use, at a time when the rvalue
would not be available."

I only know that rvalue is a temporary value that will not be used, while ref is omitting a pointer.

December 11, 2022
On 12/11/22 01:25, zjh wrote:
> On Sunday, 11 December 2022 at 04:36:45 UTC, thebluepandabear wrote:
>
>> "The main reason for this limitation is the fact that a function
>> taking a ref
>> parameter can hold on to that reference for later use, at a time when
>> the rvalue
>> would not be available."
>>
>
>
> I only know that `rvalue` is a temporary value that will not be used,

Temporary values can be used but they should not be stored for later. In this context, if the ref parameter were also 'scope', we should be able to pass rvalues. (See below.)

> while `ref` is omitting a pointer.

Yes, although it is still a pointer behind the scenes, 'ref' is syntax that avoids pointers.

However, the situation has changed in D: It has been possible to pass rvalues by reference through the magic of 'in' parameters. This currently requires the -preview=in compiler switch, which makes 'in' parameters imply 'const scope':

  https://dlang.org/spec/function.html#in-params

import std.stdio;

struct S {
    string id;
}

void foo(in S s) {
    writeln("foo received ", s.id);
}

void main() {
    auto s = S("lvalue");
    foo(s);
    foo(S("rvalue"));
}

Prints

foo received lvalue
foo received rvalue

On can add a copy constructor to S to see whether 'in' is by-copy or by-value.

Ali

December 11, 2022

On Sunday, 11 December 2022 at 04:36:45 UTC, thebluepandabear wrote:

>

Hello,

I am not really understanding why rvalues cannot be passed to a 'ref' parameter, the explanation in the book about D I am reading was not clear:

"The main reason for this limitation is the fact that a function taking a ref
parameter can hold on to that reference for later use, at a time when the rvalue
would not be available."

I didn't really understand what Ali meant by this statement, any help would be appreciated so this can be clarified.

Regards,
thebluepandabear

You can if you compile with: -preview=rvaluerefparam

I personally think this preview should be enabled by default

I use it all the time

struct vec2{ float x; float y;}

void pass(const ref vec2 pos)
{}

void main()
{
    pass( vec2(1,1) );
}

If you use dub:

           "dflags": [
                "-preview=rvaluerefparam",
            ],
December 12, 2022

On Sunday, 11 December 2022 at 16:48:57 UTC, Ali Çehreli wrote:

>

However, the situation has changed in D: It has been possible to pass rvalues by reference through the magic of 'in' parameters. This currently requires the -preview=in compiler switch, which makes 'in' parameters imply 'const scope':

https://dlang.org/spec/function.html#in-params

[...]

Thank you for your detailed explanation.