Thread overview
Class-Inheritancing Error
5 days ago
David T. Oxygen
5 days ago
H. S. Teoh
5 days ago
user1234
4 days ago
H. S. Teoh
4 days ago
David T. Oxygen
5 days ago

I wrote a piece of code like this:

class Person{
	string name;
	this(string name){this.name=name;}
}
class Someone:Person{
	alias super this;
}
void main(){
	Someone x=new Someone("Bob");
}

And then,I got a Error-Message:

ab.d(6): Error: variable name expected after type `super`, not `this`
    alias super this;
                ^
ab.d(6):        `this` is a keyword, perhaps append `_` to make it an identifier

Who knows why I can't use alias this there? Please tell me,thanks very much. I'm waiting for you.

5 days ago
On Sun, Jul 27, 2025 at 11:08:33AM +0000, David T. Oxygen via Digitalmars-d-learn wrote:
> I wrote a piece of code like this:
> 
> ```d
> class Person{
> 	string name;
> 	this(string name){this.name=name;}
> }
> class Someone:Person{
> 	alias super this;
> }
> void main(){
> 	Someone x=new Someone("Bob");
> }
> ```
> And then,I got a Error-Message:
> ```
> ab.d(6): Error: variable name expected after type `super`, not `this`
>     alias super this;
>                 ^
> ab.d(6):        `this` is a keyword, perhaps append `_` to make it an
> identifier
> 
> ```
> Who knows why I can't use `alias this` there? Please tell me,thanks
> very much. I'm waiting for you.

Can you explain more why do you want to alias super to this?  A derived class already inherits its base class's members, there's no need to explicitly alias it.


T

-- 
Fact is stranger than fiction.
5 days ago
On Sunday, 27 July 2025 at 15:14:03 UTC, H. S. Teoh wrote:
> On Sun, Jul 27, 2025 at 11:08:33AM +0000, David T. Oxygen via Digitalmars-d-learn wrote:
>> I wrote a piece of code like this:
>> 
>> ```d
>> class Person{
>> 	string name;
>> 	this(string name){this.name=name;}
>> }
>> class Someone:Person{
>> 	alias super this;
>> }
>> void main(){
>> 	Someone x=new Someone("Bob");
>> }
>> ```
>> And then,I got a Error-Message:
>> ```
>> ab.d(6): Error: variable name expected after type `super`, not `this`
>>     alias super this;
>>                 ^
>> ab.d(6):        `this` is a keyword, perhaps append `_` to make it an
>> identifier
>> 
>> ```
>> Who knows why I can't use `alias this` there? Please tell me,thanks
>> very much. I'm waiting for you.
>
> Can you explain more why do you want to alias super to this?  A derived class already inherits its base class's members, there's no need to explicitly alias it.
>
>
> T

Simply because because if OP writes

```
class Person{
    string name;
    this(string name){this.name=name;}
}
class Someone:Person{
}
void main(){
    Someone x=new Someone("Bob");
}
```

then he gets rewarded with

> Error: class `Someone` cannot implicitly generate a default constructor when base class `Person` is missing a default constructor

4 days ago
On Sun, Jul 27, 2025 at 05:02:35PM +0000, user1234 via Digitalmars-d-learn wrote: [...]
> Simply because because if OP writes
> 
> ```
> class Person{
>     string name;
>     this(string name){this.name=name;}
> }
> class Someone:Person{
> }
> void main(){
>     Someone x=new Someone("Bob");
> }
> ```
> 
> then he gets rewarded with
> 
> > Error: class `Someone` cannot implicitly generate a default constructor when base class `Person` is missing a default constructor

Oh I see.  So just write a forwarding ctor:

```
class Someone : Person {
	this(string name) { super(name); }
	...
}
```

Or use the mixin template I wrote in the other thread to auto-generate forwarding ctors, if you have many subclasses that need forwarding.


T

-- 
If you can't deal with your problems, try becoming a school bus driver.  Then all your problems will be behind you.
4 days ago

On Monday, 28 July 2025 at 18:02:45 UTC, H. S. Teoh wrote:

>

On Sun, Jul 27, 2025 at 05:02:35PM +0000, user1234 via Digitalmars-d-learn wrote: [...]

>

Simply because because if OP writes

class Person{
    string name;
    this(string name){this.name=name;}
}
class Someone:Person{
}
void main(){
    Someone x=new Someone("Bob");
}

then he gets rewarded with

>

Error: class Someone cannot implicitly generate a default constructor when base class Person is missing a default constructor

Oh I see. So just write a forwarding ctor:

class Someone : Person {
	this(string name) { super(name); }
	...
}

Or use the mixin template I wrote in the other thread to auto-generate forwarding ctors, if you have many subclasses that need forwarding.

T
Thank you very much.
As "user1234" said, I just wanted to inheritance the constructor method in the baseclass.
I have already tried your way to solve the problem,it could work. But I still want to know why my code could raise an error.And what's your mixin-template? Would you be so kind to show me this template? I would be grateful if you can share it.