DIP 1016--ref T accepts r-values--Formal Assessment
Steven Schveighoffer
schveiguy at gmail.com
Thu Jan 31 18:31:22 UTC 2019
On 1/31/19 11:04 AM, Olivier FAURE wrote:
> On Thursday, 31 January 2019 at 02:10:05 UTC, Manu wrote:
>> I still can't see a truck-sized hole.
>
> I don't know if it's truck-sized, but here's another corner case:
>
> int doubleMyValue(ref int x) {
> x *= 2;
> return x;
> }
>
> Point pt;
> pt.x = 5;
> pt.y = foobar();
>
> doubleMyValue(pt.x);
> assert(pt.x == 10);
>
> Question: in the above code, will the assertion pass?
>
> Answer: it depends on Point's implementation. If x is a member variable,
> then yes. If it's a getter, then doubleMyValue will take a rvalue and x
> won't be mutated and the assertion will fail.
>
> I think this is a non-trivial conceptual problem.
BTW, the DIP discusses how to annotate these rare situations:
int doubleMyValue(ref int x) { ... }
@disable int doubleMyValue(int x);
-Steve
More information about the Digitalmars-d-announce
mailing list