interface function overloading

%u e at ee.com
Sun Jan 9 04:22:36 PST 2011


== Quote from bearophile (bearophileHUGS at lycos.com)'s article
> %u:
> >   func(cast(I2)(new C()));
> That code smells a bit (http://en.wikipedia.org/wiki/Code_smell ).
> Bye,
> bearophile

Extract the construction and you get:

----
module main;

interface I1{}
interface I2 : I1{}

class C : I2{
  this(){}
}

void func(I1 i){}
void func(I2 i){}

void main(){
  C c = new C();
  func( cast(I2)c );
}
----

What is the deeper problem in this little snippet?
Or do you mean there is something wrong if you encounter this pattern.

I don't think it's really that much worse than renaming one(or both) of the funcs.
It forces you to cast all class:I2 objects to the func you want called, but all
class:I1 objects already call the correct func.

I think different named funcs is a better solution, but I don't think the cast
solution exemplifies a pattern of indication to a deeper problem.


More information about the Digitalmars-d-learn mailing list