www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - aliased function as overloads/etc.

Several times I have found my self wanting to alias a function. Several uses of
this idea have come to mind. The first use would allow for the addition of
attributes to a function from a derived class. The specific case I have wanted
to use was to add the final attribute to a function. Another use would be to
reuse an existing function when implementing a template. 

Thought, comments, critics??

interface I1
{
int fn1(int);
int fn2(int);
}

//     implement I1.fn1 and I2.fn2 differently
//     fn1 can't be final b/c C2 must overload
class C1: I1
{
int fn1(int i) { return i; }  // does something
int fn2(int i) { return i+1;} // does something else
}

//     overload C1.fn1
class C2: C1
{
int fn1(int i) { return i+1; }
}

//     use C1.fn1 as C3.fn1 but w/ final
class C3: C1
{
alias fn1(int i) final C1.fn1;
}

//     implement I1.fn1 and I2.fn2 as the same function
class C4: I1
{
int fn1(int i) { return i; }//  does something
alias fn1(int) fn2;//           is the same function as fn1(int)
}
Oct 24 2005