D idioms list

Artur Skawina via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Thu Jan 8 13:25:11 PST 2015


On 01/08/15 21:23, ketmar via Digitalmars-d-announce wrote:
> i'm not sure, but maybe it worth renaming "struct inheritance" to
> "extending a struct"? or even something completely different. what it
> does is actually extending/augmenting the struct, but not
> OO-inheritance, as one cannot pass "augmented" struct to the function
> which expects original struct. at least without hackery.

'alias this' is just the D syntax for implicit conversions.
The feature /is/ crippled, but there's no need for "hackery";
at least not for simple things like that.

   struct A { int a; }
   struct B { A a; alias a this; string b; }

   int f(A a) { return a.a+1; }
   int g(ref A a) { return a.a+1; }
   ref A h(ref A a) { return a; }

   int main() {
      B b;
      return f(b)+g(b)+h(b).a;
   }

artur


More information about the Digitalmars-d-announce mailing list