My Reference Safety System (DIP???)

via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 2 12:27:46 PST 2015


On Sunday, 1 March 2015 at 23:56:02 UTC, Zach the Mystic wrote:
> On Sunday, 1 March 2015 at 14:40:54 UTC, Marc Sch├╝tz wrote:
>> I don't think a callee-based solution can work:
>>
>>    class T {
>>        void doSomething() scope;
>>    }
>>    struct S {
>>        RC!T t;
>>    }
>>    void main() {
>>        auto s = S(RC!T()); // `s.t`'s refcount is 1
>>        T t = s.t;          // borrowing from the RC wrapper
>>        foo(s);
>>        t.doSomething();    // oops, `t` is gone
>>    }
>>    void foo(ref S s) {
>>        s.t = RC!T();       // drops the old `s.t`
>>    }
>
> I thought of this, and I disagree. The very fact of assigning 
> to `T t` adds the reference count you need to keep `s.t` from 
> disintegrating. As soon as you borrow, you increment the count.

Sorry, my mistake, should have explained what I have in mind.

`S.t` has type `RC!T`, but we're assigning it a variable of type 
`T`. This is made possible because `RC!T` has an `alias this` 
wrapper that returns `scope T`. The effect is that we're 
implicitly borrowing the `T` reference, as if the variable were 
declared `scope T`. The borrow checker (which I will specify 
later, see the examples [1] for a foretaste) will prohibit any 
unsafe use that would make the reference `t` outlive `s`.

Therefore, no postblit is called, and no reference count is 
incremented.

[1] http://wiki.dlang.org/User_talk:Schuetzm/scope2


More information about the Digitalmars-d mailing list