Cannot Call Super-Class Overloaded Function If Class has Function Override?
vit
vit at vit.vit
Tue Mar 1 09:01:14 UTC 2022
On Tuesday, 1 March 2022 at 08:40:12 UTC, Vijay Nayar wrote:
> I've randomly encountered a strange error, and I cannot find
> any explanation for it in the official documentation of syntax.
>
> Essentially, a class cannot call function overload in a
> super-class if the class itself contains an override. Is this a
> bug? Is this on purpose? Take a look at `B.otherThing()` below.
>
> ``` d
> void main()
> {
> abstract class A {
> // An abstract method for sub-classes
> abstract void doThing(string a, size_t b);
> // A convenience helper.
> void doThing(string a) {
> doThing(a, a.length);
> }
> }
>
> class B : A {
> // If this overload exists, something strange happens...
> override
> void doThing(string a, size_t b) {
> }
>
> void otherThing() {
> // Error: `B.doThing(string a, ulong b)`
> // is not callable using argument
> // types `(string)`
> doThing("hello");
>
> super.doThing("hello"); // OK
> }
> }
> }
> ```
```d
void main()
{
abstract class A {
// An abstract method for sub-classes
abstract void doThing(string a, size_t b);
// A convenience helper.
void doThing(string a) {
doThing(a, a.length);
}
}
class B : A {
// If this overload exists, something strange happens...
override
void doThing(string a, size_t b) {
}
alias doThing = typeof(super).doThing; //add super.doThing to
overload set.
void otherThing() {
// Error: `B.doThing(string a, ulong b)`
// is not callable using argument
// types `(string)`
doThing("hello");
super.doThing("hello"); // OK
}
}
}
```
More information about the Digitalmars-d-learn
mailing list