Why are overloaded member functions only automatically inherited if none of them are overridden in D?

In D, suppose I have a case equivalent to the following example:

import std.stdio;
import std.conv;

class first{

        int func(int x){
                return x;
        }

        int func(double x){
                return to!(int)(x+0.5);
        }
}

class second : first{
        override int func(int x){
                return x/2;
        }
}


unittest{
        second blah = new second();
        writeln(blah.func(4.5));
}

Note that in the class, “func” is first overloaded, and the second class overrides the version of func that accepts int, but not the version that accepts double.

This code does not compile with the message that func (double) is not inherited:

tmp.d(24): Error: function tmp.second.func (int x) is not callable using 
argument types (double)

However, func (double) is inherited if I remove the override function from the second class. After that, the code should compile and run as expected.

Removing a call to the unloaded overloaded function tmp.func (4.5) leads to another compilation error:

tmp.d(15): Deprecation: class tmp.second use of tmp.first.func(double x) hidden 
by second is deprecated. Use 'alias first.func func;' to introduce base class 
overload set.

, first.func func , , , "" .

, , D . :

1. , , , ?

2. - ?

+4
1

: http://dlang.org/hijack.html

, ++ , , , , , , , , , , , , , - , . , , .

, , . " ", . , . , , , . , ( super , ), , . , , , , .

, - . , , , -

alias foo = bool X.foo(int a, float b) const;

( X - )

bool foo(int a, float b) const { return super(a, b); }

, , .

+3

All Articles