Why can't structs be derived from?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Mar 16 08:31:05 PDT 2011


On 03/16/2011 10:28 AM, Steven Schveighoffer wrote:
> On Wed, 16 Mar 2011 11:06:40 -0400, Ary Manzana <ary at esperanto.org.ar>
> wrote:
>
>> On 3/15/11 4:06 PM, Steven Schveighoffer wrote:
>>> On Tue, 15 Mar 2011 14:54:13 -0400, Jens <jne at somewhere.org> wrote:
>>>
>>>> I didn't ask how to do composition in D. I asked why composition cannot
>>>> be done via derivation, i.e., the reasoning behind the language design
>>>> choice. A design faux paus IMO.
>>>
>>> Because composition by inheritance can be *completely* implemented using
>>> alias this -- a feature that also provides other niceties. Why provide
>>> another mechanism to do the exact same thing, just because you are used
>>> to it?
>>>
>>> Note that "inheritance" is actually done exactly this way in C++, by
>>> putting the derived type at the front of the "derived" type, and
>>> aliasing all the methods/fields into the derived namespace.
>>>
>>> Show me what composition by derivation provides that alias this does
>>> not.
>>
>> Oh, come on, why use alias this when you can implement it in assembler?
>
> There is a huge difference. We don't use the same syntax for polymorphic
> inheritance for struct inheritance because we do not want to confuse the
> developer.
>
> Essentially, the syntax looks better, but it looks exactly like
> polymorphic class/interface inheritance. The two types of extension are
> drastically different, and I think the syntax should reflect that.
>
> Besides, we are not talking the difference between assembler and
> high-level code here. We're talking the difference between X : Y and X {
> Y y; alias y this;}
>
> -Steve

Not to mention that alias this offers the ability of a runtime hook; you 
don't need structural conformance as you can produce the conversion on 
the fly.

Andrei


More information about the Digitalmars-d mailing list