DIP 1021--Argument Ownership and Function Calls--Community Review Round 1

Mike Franklin slavo5150 at yahoo.com
Mon Jul 22 00:05:12 UTC 2019


On Sunday, 21 July 2019 at 09:34:42 UTC, Walter Bright wrote:
> On 7/19/2019 1:32 AM, Mike Franklin wrote:
>> How can the compiler statically determine whether `foo` and 
>> `bar` will return a reference to the same data?
>
> It can't.
>
>
>> It seems the language or type system must provide this 
>> guarantee, which would require a full-fledged 
>> ownership/borrowing system to accurately enforce what this DIP 
>> is proposing.
>
> A full fledged one disallows accessing mutable global state, 
> and now you know why :-)

But it's the same issue even without the global state:

--- someLibrary.d
module someLibrary;

import core.stdc.stdlib;

struct S
{
     int a = 1;
     int b = 2;

     ref int foo() @safe
     {
         if ((rand() % 2) == 0)
             return a;
         else
             return b;
     }

     ref int bar() @safe
     {
         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());
}

I still don't see how the compiler can know statically whether or 
not `foo()` and `bar()` will return a reference to the same data.

>
> (Because the function signature does not tell the complete 
> story when the function accesses mutable global state.)

That's why a full ownership/borrowing system seems necessary 
because with such a system (either through the type system or the 
language's semantics or both), there is only ever one mutable 
reference to any data, and therefore, attribution of functions 
and/or function parameters is not required.

It appears a full ownership/borrowing system would make what this 
DIP is proposing obsolete, and is the only way to properly do 
what this DIP is proposing.

Mike


More information about the Digitalmars-d mailing list