Thread overview
alias to connect with superclass's constructor
Mar 22
monkyyy
March 22

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:

    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.

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);
}
March 22

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:

    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.

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?

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);
}
March 22
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

March 22
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.
March 22
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




March 23
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
March 24

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

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

March 24

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

5 days ago
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...