important proposal: scope keyword for class members

Christopher Wright dhasenan at gmail.com
Sat Mar 7 05:07:37 PST 2009


dsimcha wrote:
> 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.

scope can't use polymorphism. It's for situations in which you need your 
polymorphic type to be on the stack for whatever reason, but you don't 
need polymorphism at the moment.

For example, you want to open a file. File inherits from some sort of 
Stream class; it's not a struct. So you can write:

auto file = new File(path);
scope(exit) file.close;

Or:
scope file = new File(path);



More information about the Digitalmars-d mailing list