Improvement to struct inheritance possible?

Danilo codedan at aol.com
Thu Jan 18 07:10:49 UTC 2024


The following code does not work, and I think it may be a bug or 
could need an enhancement:

```d
module app;
import std;

void main() {
     auto x = new Derived(10, 20);
}

struct Base {
     int a;
}

struct Derived
{
     Base base;
     int b;
     alias this = base;

     @disable this();

     // `a` is not available for use with constructor parameters.
     //
     this( typeof(a) _a, typeof(b) _b ) {
         //
         // inside the body of the constructor, `a` is available!
         //
         a = _a;
         b = _b;

         writeln( a, ", ", b );
     }
}
```

The use of `typeof(a)` as a constructor parameter gives:
Error: undefined identifier `a`

Instead I have to explicitely use:
```d
this( typeof(this.a) _a, typeof(b) _b )
// or
this( typeof(base.a) _a, typeof(b) _b )
```

I think this may be a bug, because `base` was imported into the 
scope using `alias this = base` and should therefore be available 
as plain `a`.

With class inheritance everything works as expected:
```d
module app;

import std;

class A {
     int x;
}

class B : A {
     int y;
}

class C : B {
     int z;

     this( typeof(x) _x, typeof(y) _y, typeof(z) _z) {
         writeln(
             _x, ", ",
             _y, ", ",
             _z
         );
     }
}

void main() {
     new C(10, 20, 30);
}
```

Could this please get improved a little bit, so "inherited" 
structure members using `alias this` are already available for 
constructor paramaters?

Thanks!


More information about the Digitalmars-d mailing list