I'd like to propose a new use for the 'scope' keyword within an aggregate body.
Members of class type declared with the scope keyword are allocated not as
references or pointers, but initialized directly inside the container. Instead
of a default initializer of 'null', it will initialize with the default
constructor of the class, or an optional assignment after the declaration. Any
'new [type]' within the assignment will resolve to a simple call to the type's
__ctor, instead of a memory allocation.
Example:
class Inner {
this() {}
this(int x) {}
}
class Container {
Inner i1; // initialized to null
scope Inner i2; // allocated within class
scope i3 = new Inner(42); // allocated within class
this() {
// implicit i2.__ctor();
// i3.__ctor(42) written above, executed here
assert(i1 is null);
assert(i2 !is null);
i1 = new Inner;
}
this(int overloaded) {
// i2 and i3 are constructed, same as above
}
~this() {
// implicit i2.__dtor();
// implicit i3.__dtor();
// i1 is still somewhere in the heap
}
}
IN ENGLISH:
If it's likely that class members will be constructed with and die with the
object, why not just allocate them right in the class? Save on heap
fragmentation and cache misses. I was honesetly flabberghasted when I realized
there was no way to do this in D, it seems like it should be one of the most
basic constructs of a C-derived (or any low-ish level) language.
The programmer knows best where he wants his objects stored. Also, the 'scope'
makes sense, and we should mirror the current behavior of the keyword in
function bodies.