DIP 1021--Argument Ownership and Function Calls--Community Review Round 1
David Bennett
davidbennett at bravevision.com
Sun Jul 21 23:57:38 UTC 2019
On Friday, 19 July 2019 at 08:01:50 UTC, Walter Bright wrote:
> On 7/18/2019 7:59 PM, David Bennett wrote:
>
>> Also, does it also check simple non-heap references, for
>> example would this become compile time error?
>>
>> ---
>> @safe void f(scope ref int a, scope ref int b)
>> {
>> a=1;
>> assert(b==1);
>> }
>> @safe void main()
>> {
>> int i = 0;
>> f(i, i);
>> assert(i==1);
>> }
>> ---
>
> Yes, because you are passing two mutable references to the same
> memory.
>
> ...
>
> Since the DIP disallows passing two mutable pointers to the
> same memory object, any code that does will break.
Thanks for clearing that up, it was not obvious what a "mutable
pointer" was in that context as in this example only the data
appeared to be mutable and not the pointer.
With that in mind would passing 2 slices to overlaping data cause
a compile time error?
---
@safe void f(scope int[] a, scope int[] b)
{
a[1] = 1;
assert(b[0]==1);
}
@safe void main()
{
int[] i = new int[32];
f(i[0..$], i[1..$]);
assert(i[1]==1);
}
---
Sorry for all the questions, I believe this DIP or something link
it is a good step in the right direction. I'm just trying to
update my mental model of D so I can write code with this in mind
today and have a better understanding.
More information about the Digitalmars-d
mailing list