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

Reply via email to