DIP 1021--Argument Ownership and Function Calls--Community Review Round 1
Walter Bright
newshound2 at digitalmars.com
Wed Jul 31 09:25:26 UTC 2019
On 7/19/2019 9:39 AM, Timon Gehr wrote:
> @safe:
> class C{
> Array a;
> this(Array a){ this.a=move(a); }
> }
>
> void maybe_bad(C c, ref int i){
> c.a = Array();
> i++;
> }
>
> void main(){
> auto c=new C(Array());
> c.a.append(5);
> auto d=c;
> maybe_bad(c,d.a.get());
> }
If Array was ref-counted, then it will be safe. The problem with ref-counted
objects, as you pointed out years ago, is this:
maybe_bad(c,c.a.get());
Copying c to d will increase the ref count and so it will be safe.
Without this DIP, ref-counted objects can't be safe. With it, they can.
More information about the Digitalmars-d
mailing list