digitalmars.D.learn - alias to connect with superclass's constructor
- Andy Valencia (42/42) Mar 21 Consider the following, totally contrived, code. The compiler
- monkyyy (29/71) Mar 21 this?
- =?UTF-8?Q?Ali_=C3=87ehreli?= (9/17) Mar 22 And then it's more meaningful to me for the other constructor to call
- Paul Backus (2/7) Mar 22 It's never worked. The error message is wrong.
- Jonathan M Davis (7/16) Mar 22 Well, that syntax should work with other member functions. So, I would g...
- Nick Treleaven (3/20) Mar 23 Simple fix to not suggest alias when its a hidden constructor:
- Menjanahary R. R. (2/20) Mar 26 Mind bending...
- Salih Dincer (22/32) Mar 23 When you add an empty constructor, the code still runs without an
- Andy Valencia (6/8) Mar 24 Yes, I had already made it run. I was asking about making it run
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
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
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 setsYeah, 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
On Saturday, 22 March 2025 at 12:05:11 UTC, Ali Çehreli wrote:On 3/21/25 8:35 PM, Andy Valencia wrote:It's never worked. The error message is wrong.tst39.d(21): add `alias this = tst39.A.this` to`tst39.B`'s bodyto merge the overload setsYeah, that doesn't work. Perhaps a regression...
Mar 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: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 DavisOn 3/21/25 8:35 PM, Andy Valencia wrote:It's never worked. The error message is wrong.tst39.d(21): add `alias this = tst39.A.this` to`tst39.B`'s bodyto merge the overload setsYeah, that doesn't work. Perhaps a regression...
Mar 22
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:Simple fix to not suggest alias when its a hidden constructor: https://github.com/dlang/dmd/pull/21069On Saturday, 22 March 2025 at 12:05:11 UTC, Ali Çehreli wrote: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.On 3/21/25 8:35 PM, Andy Valencia wrote:It's never worked. The error message is wrong.tst39.d(21): add `alias this = tst39.A.this` to`tst39.B`'s bodyto merge the overload setsYeah, that doesn't work. Perhaps a regression...
Mar 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:Mind bending...On Saturday, 22 March 2025 at 12:05:11 UTC, Ali Çehreli wrote: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 DavisOn 3/21/25 8:35 PM, Andy Valencia wrote:It's never worked. The error message is wrong.tst39.d(21): add `alias this = tst39.A.this` to`tst39.B`'s bodyto merge the overload setsYeah, that doesn't work. Perhaps a regression...
Mar 26
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
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 errorYes, 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