Christopher Wright Wrote: > 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);
Exactly. And on top of that, how does being 'scope' limit its polymorphism? Code time: int main(char[][] args) { class A {} class B : A {} class C : B {} void tryB(A a) {writefln(cast(B)a ? "casted" : "null");} scope a = new A; scope b = new B; assert(cast(C)b is null); // OK assert(cast(A)b == b); // OK tryB(a); // "null" tryB(b); // "casted" return 42; } You can always upcast and downcast safely, since it isn't actually a 'value' type, only the address is passed around. The type of the class remains intact. So I don't see why this shouldn't be extended to classes as aggregate members.