Thread overview
Accessing a function within an object's superclass from the outside
Jan 14, 2017
David Zhang
Jan 14, 2017
ketmar
Jan 14, 2017
David Zhang
Jan 14, 2017
ketmar
Jan 14, 2017
Ali Çehreli
Jan 14, 2017
Adam D. Ruppe
Jan 15, 2017
Meta
Jan 15, 2017
Adam D. Ruppe
January 14, 2017
Hello,

Say I have a class, and it overrides a function from its superclass. However, the superclass has two overloaded versions of the same function. My intent is to override only one of the functions, but the second is rendered inaccessible from outside of the class. How can I make the overloaded function visible?

ie:

class ClassA {
    void fun(uint a) {}
    void fun(uint a, float b) {}
}

class ClassB: ClassA {
    void fun(uint a) {}
}

ca.fun(a);       //ok
ca.fun(a, b);    //ok
cb.fun(a);       //ok
cb.fun(a, b);    //function fun not callable with uint and float

I seem to remember something about using aliases to fix this, but I can't find anything about it either way.
January 14, 2017
On Saturday, 14 January 2017 at 21:55:27 UTC, David  Zhang wrote:
> Hello,
>
> Say I have a class, and it overrides a function from its superclass. However, the superclass has two overloaded versions of the same function. My intent is to override only one of the functions, but the second is rendered inaccessible from outside of the class. How can I make the overloaded function visible?
>
> ie:
>
> class ClassA {
>     void fun(uint a) {}
>     void fun(uint a, float b) {}
> }
>
> class ClassB: ClassA {
>     void fun(uint a) {}
> }
>
> ca.fun(a);       //ok
> ca.fun(a, b);    //ok
> cb.fun(a);       //ok
> cb.fun(a, b);    //function fun not callable with uint and float
>
> I seem to remember something about using aliases to fix this, but I can't find anything about it either way.

class ClassB: ClassA {
  alias fun = super.fun;
  override void fun(uint a) {}
}
January 14, 2017
On Saturday, 14 January 2017 at 22:17:23 UTC, ketmar wrote:
> class ClassB: ClassA {
>   alias fun = super.fun;
>   override void fun(uint a) {}
> }

I tried that, but it seems to think I mean to override super.fun(uint) instead of super.fun(uint, float).

Looking at my code again, one of them is templated with a range interface. I think that might be the problem, though I can't figure out how to fix it. Wrapping the alias in a template block doesn't seem to do it.
January 14, 2017
On Saturday, 14 January 2017 at 22:38:15 UTC, David  Zhang wrote:
> On Saturday, 14 January 2017 at 22:17:23 UTC, ketmar wrote:
>> class ClassB: ClassA {
>>   alias fun = super.fun;
>>   override void fun(uint a) {}
>> }
>
> I tried that, but it seems to think I mean to override super.fun(uint) instead of super.fun(uint, float).

ahem?
`void fun(uint a)` == `void fun(uint a)`
`void fun(uint a)` != `void fun(uint, float)`
of course, you mean to override `void fun(uint a)` with this code.
January 14, 2017
Templates are not virtual. Depending the interface, a different function is called:

import std.stdio;

class ClassA {
    void fun(T)(T a) { writeln("ClassA"); }
}

class ClassB: ClassA {
    void fun(uint a) { writeln("ClassB"); }
}

void main() {
    auto cb = new ClassB();
    ClassA ca = cb;

    uint a = 42;
    ca.fun(a);    // calls ClassA.fun
    cb.fun(a);    // calls ClassB.fun
}

Ali

January 14, 2017
On Saturday, 14 January 2017 at 21:55:27 UTC, David  Zhang wrote:
> I seem to remember something about using aliases to fix this, but I can't find anything about it either way.

So you can alias the names together to merge the overload sets, or at the call site, you can also specify which class's version you want with a dot:

cb.ClassA.fun(a, b); // compiles, specifically calls the ClassA method
January 15, 2017
On Saturday, 14 January 2017 at 23:31:53 UTC, Adam D. Ruppe wrote:
> On Saturday, 14 January 2017 at 21:55:27 UTC, David  Zhang wrote:
>> I seem to remember something about using aliases to fix this, but I can't find anything about it either way.
>
> So you can alias the names together to merge the overload sets, or at the call site, you can also specify which class's version you want with a dot:
>
> cb.ClassA.fun(a, b); // compiles, specifically calls the ClassA method

Is this documented anywhere? I had no idea this was a feature.
January 15, 2017
On Sunday, 15 January 2017 at 02:32:29 UTC, Meta wrote:
> Is this documented anywhere? I had no idea this was a feature.

Used in some examples here:
http://dlang.org/spec/class.html