https://dlang.org/spec/function.html#function-inheritance

Consider this snippet from the documentation:

class A
{
    int foo(int x) { ... }
    int foo(long y) { ... }
}

class B : A
{
    override int foo(long x) { ... }
}

void test()
{
    B b = new B();
b.foo(1); // calls B.foo(long), since A.foo(int) not considered
    A a = b;

a.foo(1); // issues runtime error (instead of calling A.foo(int))
}


I ran into this the other day, where I had a function of the same name in a child class, and found that all functions in the parent of the same name now became hidden, unless I add an alias statement.

After a bit of reading, I understood the rule and how it works, but what I'm missing is the "why". Why is it desirable to hide methods from a parent class which have the same name (but different arguments) as a method in a class?

Reply via email to