www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias to connect with superclass's constructor

reply Andy Valencia <dont spam.me> writes:
Consider the following, totally contrived, code.  The compiler 
tells me:

tst39.d(21): Error: constructor `tst39.B.this(string s)` is not 
callable using argument types `()`
tst39.d(21):        constructor `tst39.B.this` hides base class 
function `tst39.A.this`
tst39.d(21):        add `alias this = tst39.A.this` to 
`tst39.B`'s body to merge the overload sets

But I haven't yet found a way to use this guidance to resolve the 
error.  I _can_ add this to B:

```d
     this() {
         super():
     }
```

But I'm curious if there's a working example of getting this 
effect with the compiler-recommended alias usage?  My searches 
here and on the Duck have come up empty.

```d
class A {
     int a;

     this() {
         this.a = 1;
     }
}
class B : A {
     string b;

     this(string s) {
         super();
         this.b = s;
     }
}
void
main() {
     import std.stdio : writeln;

     auto a = new A();
     auto b1 = new B();
     auto b2 = new B("Hi, Mom!");
     writeln(b1.a, " - ", b1.b);
     writeln(b2.a, " - ", b2.b);
}
```
Mar 21
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Saturday, 22 March 2025 at 03:35:35 UTC, Andy Valencia wrote:
 Consider the following, totally contrived, code.  The compiler 
 tells me:

 tst39.d(21): Error: constructor `tst39.B.this(string s)` is not 
 callable using argument types `()`
 tst39.d(21):        constructor `tst39.B.this` hides base class 
 function `tst39.A.this`
 tst39.d(21):        add `alias this = tst39.A.this` to 
 `tst39.B`'s body to merge the overload sets

 But I haven't yet found a way to use this guidance to resolve 
 the error.  I _can_ add this to B:

 ```d
     this() {
         super():
     }
 ```

 But I'm curious if there's a working example of getting this 
 effect with the compiler-recommended alias usage?  My searches 
 here and on the Duck have come up empty.

 ```d
 class A {
     int a;

     this() {
         this.a = 1;
     }
 }
 class B : A {
     string b;

     this(string s) {
         super();
         this.b = s;
     }
 }
 void
 main() {
     import std.stdio : writeln;

     auto a = new A();
     auto b1 = new B();
     auto b2 = new B("Hi, Mom!");
     writeln(b1.a, " - ", b1.b);
     writeln(b2.a, " - ", b2.b);
 }
 ```
this? ```d class A { int a; this() { this.a = 1; } } class B : A { string b; this(A...)(A args){ super(args); } this(string s) { super(); this.b = s; } } void main() { import std.stdio : writeln; auto a = new A(); auto b1 = new B(); auto b2 = new B("Hi, Mom!"); writeln(b1.a, " - ", b1.b); writeln(b2.a, " - ", b2.b); } ```
Mar 21
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=C3=87ehreli?= <acehreli yahoo.com> writes:
On 3/21/25 8:35 PM, Andy Valencia wrote:

 tst39.d(21):        add `alias this = tst39.A.this` to `tst39.B`'s body
 to merge the overload sets
Yeah, that doesn't work. Perhaps a regression...
 I _can_ add this to B:

 ```d
      this() {
          super():
      }
 ```
And then it's more meaningful to me for the other constructor to call this() instead of super() directly: this(string s) { this(); this.b = s; } Ali
Mar 22
parent reply Paul Backus <snarwin gmail.com> writes:
On Saturday, 22 March 2025 at 12:05:11 UTC, Ali Çehreli wrote:
 On 3/21/25 8:35 PM, Andy Valencia wrote:

 tst39.d(21):        add `alias this = tst39.A.this` to
`tst39.B`'s body
 to merge the overload sets
Yeah, that doesn't work. Perhaps a regression...
It's never worked. The error message is wrong.
Mar 22
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Saturday, March 22, 2025 11:38:05 AM MDT Paul Backus via Digitalmars-d-learn
wrote:
 On Saturday, 22 March 2025 at 12:05:11 UTC, Ali Çehreli wrote:
 On 3/21/25 8:35 PM, Andy Valencia wrote:

 tst39.d(21):        add `alias this = tst39.A.this` to
`tst39.B`'s body
 to merge the overload sets
Yeah, that doesn't work. Perhaps a regression...
It's never worked. The error message is wrong.
Well, that syntax should work with other member functions. So, I would guess that the compiler is just giving the same error message that it normally gives when this happens with member functions, whereas since it doesn't work with constructors, it really should have a different error message, but it doesn't. - Jonathan M Davis
Mar 22
next sibling parent Nick Treleaven <nick geany.org> writes:
On Sunday, 23 March 2025 at 05:40:32 UTC, Jonathan M Davis wrote:
 On Saturday, March 22, 2025 11:38:05 AM MDT Paul Backus via 
 Digitalmars-d-learn wrote:
 On Saturday, 22 March 2025 at 12:05:11 UTC, Ali Çehreli wrote:
 On 3/21/25 8:35 PM, Andy Valencia wrote:

 tst39.d(21):        add `alias this = tst39.A.this` to
`tst39.B`'s body
 to merge the overload sets
Yeah, that doesn't work. Perhaps a regression...
It's never worked. The error message is wrong.
Well, that syntax should work with other member functions. So, I would guess that the compiler is just giving the same error message that it normally gives when this happens with member functions, whereas since it doesn't work with constructors, it really should have a different error message, but it doesn't.
Simple fix to not suggest alias when its a hidden constructor: https://github.com/dlang/dmd/pull/21069
Mar 23
prev sibling parent Menjanahary R. R. <megnany afaky.com> writes:
On Sunday, 23 March 2025 at 05:40:32 UTC, Jonathan M Davis wrote:
 On Saturday, March 22, 2025 11:38:05 AM MDT Paul Backus via 
 Digitalmars-d-learn wrote:
 On Saturday, 22 March 2025 at 12:05:11 UTC, Ali Çehreli wrote:
 On 3/21/25 8:35 PM, Andy Valencia wrote:

 tst39.d(21):        add `alias this = tst39.A.this` to
`tst39.B`'s body
 to merge the overload sets
Yeah, that doesn't work. Perhaps a regression...
It's never worked. The error message is wrong.
Well, that syntax should work with other member functions. So, I would guess that the compiler is just giving the same error message that it normally gives when this happens with member functions, whereas since it doesn't work with constructors, it really should have a different error message, but it doesn't. - Jonathan M Davis
Mind bending...
Mar 26
prev sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Saturday, 22 March 2025 at 03:35:35 UTC, Andy Valencia wrote:
 Consider the following, totally contrived, code.  The compiler 
 tells me:

 tst39.d(21): Error: constructor `tst39.B.this(string s)` is not 
 callable using argument types `()`
 tst39.d(21):        constructor `tst39.B.this` hides base class 
 function `tst39.A.this`
 tst39.d(21):        add `alias this = tst39.A.this` to 
 `tst39.B`'s body to merge the overload sets

 But I haven't yet found a way to use this guidance to resolve 
 the error.  I _can_ add this to B:
When you add an empty constructor, the code still runs without an error ```d class Counter : StaticCounter { string msg;    this(string str) { msg = str; }    this() {} // added Salih } class StaticCounter { static int id; this() { ++id; } } import std.stdio : writeln; void main() {    with ( new Counter() )        id.writeln(": ", msg);    with ( new Counter("Hi, Mom!") )        id.writeln(": ", msg); } ``` SDB 79
Mar 23
parent Andy Valencia <dont spam.me> writes:
On Monday, 24 March 2025 at 03:46:25 UTC, Salih Dincer wrote:

 When you add an empty constructor, the code still runs without 
 an error
Yes, I had already made it run. I was asking about making it run using aliases which, as it turns out, you can't. I'm grateful that there's even a PR in the works to update the compiler's error message. Andy
Mar 24