On Wednesday, 2 December 2015 at 15:58:19 UTC, Andrei
Alexandrescu wrote:
On 12/02/2015 01:45 AM, deadalnix wrote:
On Tuesday, 1 December 2015 at 17:27:20 UTC, Andrei
Alexandrescu wrote:
Ah, the good old assignment to reference. We need to prevent
that from
happening in safe code. Got any fresh ideas? -- Andrei
Disable owner when borrowing 'mutably', and not when borrowing
'constly'.
What does "disable owner" mean? Thx! -- Andrei
(He probably means: The owner is the object to which the
reference points. Disabling means disallowing any access to it,
at compile time.)
But your question gave me another idea: Instead of making the
owner const, the compiler can insert calls to `owner.opFreeze()`
and `owner.opThaw()` at the beginning/end of each borrowing, and
leave the owner mutable. It's then up to the implementer to
handle things in a way they like. For example, opFreeze() could
just set a flag and assert that the underlying memory isn't freed
during borrowing, or it could increment/decrement a reference
count, or it could queue up any releases of the underlying
storage to happen after the last borrow has expired (the idea you
proposed as a solution for RCArray).
It's helpful in this case if the operators have the following
signatures:
T opFreeze();
void opThaw(T cookie);
For the refcounting solution, opFreeze() can increment the
refcount and return a pointer to it, and opThaw() can decrement
it again. The methods need to be called each time a borrow
starts/ends.