On Tuesday, 13 May 2014 at 17:41:23 UTC, Walter Bright wrote:
On 5/13/2014 6:36 AM, Dicebot wrote:
Main problem about making `ref` borrowed pointer is that you
will need to
prohibit storing it in function transitively. This will need
to become invalid
code:
struct A
{
int* ptr;
}
int* gptr;
void foo(ref A a)
{
gptr = a.ptr; // error, can't leak borrowed a.ptr into
global context
}
The lifetime of &a is not at all the same as the lifetime of
a.ptr, those are independent pointers. I.e. ref is not
transitive (unlike const which is transitive).
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.