DIP 36: Rvalue References

Dicebot m.strashun at gmail.com
Wed Apr 10 00:43:56 PDT 2013


On Wednesday, 10 April 2013 at 00:41:54 UTC, kenji hara wrote:
> Why test32 does not accept rvalue like test1? I think that 
> "scope ref"
> should always behave same for rvalue argument - create 
> temporary and take
> its address to "scope ref" parameter. Consistent behavior with 
> any types is
> important for the generic programming.

That is me to blame. I have convinced Namespace that this is the 
way to go in private conversation and hope to do the same with 
you ;)

Consistent behavior is important but it does not change the fact 
that there two types of rvalues in the language - raw literals 
(42, A.init) and constructed temporaries (A()). Accepting rvalues 
of the first type in as mutable references may be consistent from 
the point of view of syntax by _extremely_ confusing 
semantics-wise:

void foo(scope ref int x)
{
     x = 32;
}

void main()
{
     foo(42); // erm, 32 is the new 42 or ref is not actually ref?
}

Beauty of "in ref" is that in that case user can't use it in any 
way that may disclose the fact the temporary variable for 
non-adressable entities is created. For mutable scope ref it is 
not the case and we must leak implementation details, which is 
never good.

Another concern is that if someone tries to pass "42" as a 
mutable ref, most likely he does not really wants it to be 
mutable and "in ref" is a better match.

This may not be consistent from the point of view of generic 
code, but it is now consistent from the point of view of 
programmer : A(...) construct always create temporaries, raw 
value literals never do (well, they will for "in ref" but you 
can't observe it). I think this is much more important.

One extra thing to note is that test32 may actually work if it 
instantiates T as "const int" for 42 (making it "in ref" 
essentially), but I don't know if D rules allow it.


More information about the Digitalmars-d mailing list