Jeremie Pelletier:

> No need to store a reference within the object, that would still require 
> another heap allocation killing the need for scope in the first place.

Probably you have not understood what I meant. I surely didn't meant that it 
needs another heap allocation (the purpose of scope is to avoid that).

Such extra invisible reference field among that object attributes is not 
necessary, but it makes things simpler. If you add such reference, then inside 
the outer object you have the inner object followed by the reference to the 
inner object, both inlined (so no other heap allocations are necessary). So you 
can keep all the usual semantics of D objects, avoid most special-casing. So 
for example you can assign a different object to such reference:

class C1 { int x, y, z; }

class C2 {
  scope C1 c1;
}

void main() {
  auto c2 = new C2;
  auto c = new C1;
  c2.c1 = c; // <--- see here
}

Now the memory of c1 inside c2 is not reachable anymore, so the destructor of 
that inner object can be called by the GC, but of course its actual memory will 
not be freed until c2 is deallocated.

Bye,
bearophile

Reply via email to