DIP 1021--Argument Ownership and Function Calls--Community Review Round 1
Mike Franklin
slavo5150 at yahoo.com
Mon Jul 22 09:28:27 UTC 2019
On Monday, 22 July 2019 at 08:45:11 UTC, Olivier FAURE wrote:
> Assuming you compile with dip1000 and dip1021, foo and bar in
> the above code would have to be marked `return` (that is,
> `this` is a `return ref`) or trigger a compile error.
Indeed. That seems to work.
> The compiler would presumably be conservative and assumes that
> they always return references to overlapping data, which is
> usually what you want (it's what the Rust compiler does, at
> least).
I'd rather not make any assumptions. Walter, what say you?
Given the code below (with added `return` lifetime annotations),
how would DIP1021 behave?
--- someLibrary.d
module someLibrary;
import core.stdc.stdlib;
struct S
{
int a = 1;
int b = 2;
ref int foo() @safe return
{
if ((rand() % 2) == 0)
return a;
else
return b;
}
ref int bar() @safe return
{
if ((rand() % 4) == 0)
return a;
else
return b;
}
}
--- main.d
import someLibrary;
void doSomething(scope ref int a, scope ref int b) @safe
{
// whatever...
}
void main() @safe
{
S s;
for(int i = 0; i < 100; i++)
doSomething(s.foo(), s.bar());
}
Thanks,
Mike
More information about the Digitalmars-d
mailing list