www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Inheriting function template from super class

reply Alexey <invalid email.address> writes:
hello.
here I have some code sample.
I'm not sure if this is my incorrect code or incorrect dmd 
behavior.
Intention is C2 to inherit C1's `void writetext(alias A1)()` and 
instantiate it with `this.writetext!(typeof(this))();`
```D
import std.stdio;

class C1
{
     void writetext()
     {
         this.writetext!(typeof(this))();
     }

     void writetext(alias A1)()
     {
         writeln("C1 writetext(alias A1) ", this);
     }
}

class C2 : C1
{
     override void writetext()
     {
         // if line 21 is commented and line 22 is uncommented - 
this compiles ok
         this.writetext!(typeof(this))();
         // super.writetext!(typeof(this))();
     }
}

void main()
{
     auto c2 = new C2;
     c2.writetext();
}

```
Sep 27 2021
next sibling parent Alexey <invalid email.address> writes:
On Monday, 27 September 2021 at 14:54:41 UTC, Alexey wrote:
 hello.
 here I have some code sample.
consequently, If I want C3 to inherit form C2, I'll have to forward template to from C1 to C2 by hand: ```D import std.stdio; class C1 { void writetext() { this.writetext!(typeof(this))(); } void writetext(alias A1)() { writeln("C1 writetext(alias A1) ", this); } } class C2 : C1 { override void writetext() { this.writetext!(typeof(this))(); } void writetext(alias A1)() { super.writetext!(A1)(); } } class C3 : C2 { override void writetext() { super.writetext!(typeof(this))(); } } void main() { auto c3 = new C3; c3.writetext(); } ```
Sep 27 2021
prev sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Monday, 27 September 2021 at 14:54:41 UTC, Alexey wrote:
 I'm not sure if this is my incorrect code or incorrect dmd 
 behavior.
This is by design, overloads only consider things declared in the same place. see here https://dlang.org/articles/hijack.html
Sep 27 2021
parent Alexey <invalid email.address> writes:
On Monday, 27 September 2021 at 16:10:54 UTC, Adam D Ruppe wrote:

 This is by design, overloads only consider things declared in 
 the same place.

 see here https://dlang.org/articles/hijack.html
thanks, Adam I'd also like to ask about similar code. If I'm writing code like so, I'm getting error. What I wanted to, is `this` to be the instance of C2. how can I do this? the second snippet is how I worked it around (explicitly defined and used template type parameter), but is there better solution? ```D import std.stdio; class C1 { void writetext() { this.writetext_x(); } void writetext_x(this T)() { const id = __traits(identifier, T); pragma(msg, "generating writetext for ", id); static assert(is(T == typeof(this))); } } class C2 : C1 { } void main() { auto c2 = new C2; c2.writetext_x(); } ``` ```D import std.stdio; class C1 { void writetext() { this.writetext_x(); } void writetext_x(T)(T new_this) { const id = __traits(identifier, T); pragma(msg, "generating writetext for ", id); } } class C2 : C1 { } void main() { auto c2 = new C2; c2.writetext_x(); } ```
Sep 27 2021