Why can't we derive struct's?

Basile B. b2.temp at gmx.com
Thu Dec 20 02:06:43 UTC 2018


On Thursday, 20 December 2018 at 01:40:02 UTC, Manu wrote:
> I've long since become bored of this design decision. Is there 
> good
> reason for it?
> Why should it be impossible to derive a struct?
>
> It can just be a sugar for an `alias this`,

Yes but another possible semantic for a struct inheritance list 
would be the structural conformance, i.e duck types, but this 
will never happen in D since we use constraints for that.

This could even be used as a primitive, simple and clean template 
system, although restricted to struct/union:

     @dummy struct Base { void required(); }
     struct Foo : Base { void required(){} }
     // we don't expect a Base as param, but something that's 
conform with Base,
     // i.e something that can be statically verified to have 
`void required();`
     void call(@dummy Base base){}

Which in D translates to much, really much verbose and complex 
code:

     struct Base { void required(); }
     struct Foo  { void required(){} }
     void call(B)(B base)
     if (__traits(hasMember, B, "required") &&
         isSomeFuncion(...) &&
         is(ReturnType!(...) == void))
     {}

let's say if you want to be 100% exact because nowadays I tend 
not to use template constraints anymore excepted when they are 
needed for the correct dispatching to an overload, they are a 
loss of time otherwise (i know some will hate this alternative 
point of view... )


More information about the Digitalmars-d mailing list