important proposal: scope keyword for class members

dsimcha dsimcha at yahoo.com
Fri Mar 6 21:33:54 PST 2009


== Quote from John Simon (zildjohn01 at gmail.com)'s article
> 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.

But the whole point of classes is that they're supposed to be polymorphic.  If you
don't need polymorphism, that's what structs are for.  You can store them either
inline (default) or in separate heap space (using pointers).  If you do need
polymorphism, you don't know at compile time what the size of the object is
supposed to be, and initializing the object the way you suggest defeats polymorphism.



More information about the Digitalmars-d mailing list