[Dlang-internal] Corner case about inheritance and template functions
Valentino Giudice
valentino.giudice96 at gmail.com
Sun Sep 6 03:21:56 UTC 2026
Hi all.
I am not certain this is the right forum.
If there is something big I'm missing, this should have been a
post for "Learn". If I found a bug, it should have been a post
for "Bugs". I'm making a guess that neither is true in this case,
and this a suggestion about language development, so I'm posting
here. I apologize if I got this wrong.
Consider the following snippet:
```D
import std.conv : text;
import std.stdio : writeln;
abstract class Class1
{
void action(T)(T a)
{
action(text(a));
}
abstract void action(string a);
}
class Class2 : Class1
{
alias action(T) = demo2.Class1.action!T; // This shouldn't be
necessary.
override void action(string a)
{
writeln(a);
}
}
void main()
{
Class2 o = new Class2();
(cast(Class1) o).action("2");
(cast(Class1) o).action!int(2);
(cast(Class1) o).action(2);
o.action("2");
o.action!int(2);
//o.action(2); // This should be allowed.
}
```
In my view, all lines should print "2".
The two comments in the code are pretty self-explanatory and
reflect how I feel about this matter.
I think, in essence, that `Class1` and `Class2` in this example
should provide the same API.
The last line of main, `o.action(2)`, is not allowed and leads to
a compilation error. The previous line, `o.action!int(2)`, is
only allowed if the alias is present in `Class2`, which I think
shouldn't be required thanks to inheritance.
If I comment out `o.action!int(2)`, uncomment the last line and
remove the alias, then the compier, when failing to compile
`o.action(2)`, suggests adding the alias. So it is weird that
adding it doesn't actually solve the problem for the last line.
The suggestion is not given for the previous line (the one that
actually does require the alias).
More information about the Dlang-internal
mailing list