Am 15.03.2011 20:40, schrieb Jens: > Daniel Gibson wrote: >> Am 15.03.2011 20:24, schrieb Jens: >>> Daniel Gibson wrote: >>>> Am 15.03.2011 19:48, schrieb Jens: >>>>> Andrei Alexandrescu wrote: >>>>>> On 3/15/11 12:55 PM, Jens wrote: >>>>>>> Steven Schveighoffer wrote: >>>>>>>> That's all there is. Structs do not have inheritance, only >>>>>>>> alias this. >>>>>>> >>>>>>> Why don't they though? Inheritance does not have to mean >>>>>>> polymorphic. It can mean composition, like in C++. I don't >>>>>>> understand the reason for such ugly syntax. >>>>>> >>>>>> Using inheritance for composition is frowned upon in C++ for good >>>>>> reasons. If you want composition, the best is to use composition. >>>>> >>>>> It was frowned upon early on because the compiler implementers >>>>> didn't have their acts together and the resulting objects layout >>>>> could not be relied upon. The example I gave came from the STL so >>>>> I think "frowned upon" is something you are picking up from long >>>>> ago. >>>>> >>>>> Composition means access through the members rather than direct >>>>> access: >>>>> >>>>> struct point >>>>> { >>>>> int x; >>>>> int y; >>>>> }; >>>>> >>>>> struct point3d >>>>> { >>>>> point pt; >>>>> int z; >>>>> }; >>>>> >>>>> ... >>>>> >>>>> point3d mypoint; >>>>> mypoint.pt.x = 3; // ugly >>>>> >>>> >>>> This is why you add "alias pt this;" to point3d. So you can write >>>> mypoint.x = 3; >>>> >>> >>> Still ugly though. >>> >> >> I don't think so. It makes obvious what happens: a composition - >> *not* an inheritance - with syntactic sugar that allows one to omit >> the .pt in mypoint.(pt.)x - as long as mypoint doesn't have a member >> x itself. >> > > YMMV. To me it's ugly enough to not take the language seriously. > >> Allowing inheritance syntax on structs would only lead to confusion - >> especially for people coming from C++. >> > > C++ does it that way, so what confusion are you talking about? >
The confusion that in C++ classes and structs are basically the same (only the default visibility of members is different) while in D they're not. So allowing "inheritance" on structs would give the false impression that structs in D are also classes of some kind. Furthermore I find C++'s class handling quite unfortunate.. only having polymorphism when explicitly using pointers really sucks. e.g. you have a base class Foo and a class Bar derived from Foo.. now you wanna put Objects of type Foo and Bar in a list.. what do you do? list<Foo>? won't work for Bar Objects. So you gotta use list<Foo*>. Using list<Foo*> really sucks, especially with iterators.. you end up using something like list<Foo*>::iterator it = ... ; (*it)->x = 3; int bla = (*it)->myFun(42); Now *that* is ugly.