borrowed pointers vs ref

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Tue May 13 11:48:14 PDT 2014


On 5/13/2014 10:52 AM, Dicebot wrote:
> It has to be transitive to be useful as borrowed pointer. Consider this example:
>
> {
>      scope A a; // has some internally managed resources
>      foo(a);
> }
>
> It is not safe to destruct a in the end of the scope here because foo may have
> stored references to a owned resources. But if foo signature is `foo(scope ref A
> a)` then compiler can statically verify that it is safe which is the very point
> of borrowing guarantees. It must be transitive to guarantee anything of course.

If those internal resources of A are marked as refcounted, then transitivity is 
not necessary. Consider also that a struct A can completely control any escaping 
references - transitive borrowing is not necessary.


More information about the Digitalmars-d mailing list