Thread overview
Inheriting function template from super class
Sep 27, 2021
Alexey
Sep 27, 2021
Alexey
Sep 27, 2021
Adam D Ruppe
Sep 27, 2021
Alexey
September 27, 2021

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))();

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();
}

September 27, 2021

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:

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();
}
September 27, 2021
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
September 27, 2021

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?

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();
}
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();
}