Walter: extend existing classes with new methods?
Marcio
mqmnews123 at sglebs.com
Wed Sep 6 09:53:15 PDT 2006
Don Clugston wrote:
> A somewhat related concept is here (very hackish C++, but the concept
> translates easily to D):
>
> http://www.codeproject.com/cpp/retrofitpolymorphism2.asp
>
> The idea being that you make a new interface, IDumpableObject,
> and define how to cast any Object into an IDumpableObject.
>
> You never actually add members to Object. Instead, you make everything
> look as though it derived from IDumpableObject, and then your framework
> uses IDumpableObject everywhere.
If you don't need to add code to the class, this works well in
statically typed languages. You are basically trying to overcome the
limitation of static typing where a 3rd-party class already declared the
interfaces it implements, so you are stuck. It's a closed module now. If
another class declares it uses another interface that looks just the
same, the 2 are still incompatible because they implement 2 separate
interfaces, even if they are really the same in terms of functionality.
This is an issue even more now with web services, and Erik Meijer's talk
mentions this issue (it seems that VB will be really flexible here)
http://channel9.msdn.com/ShowPost.aspx?PostID=223865#223865
But note that there are many cases when you need to add code as well.
http://www.cs.princeton.edu/~dpw/popl/06/Tim-POPL.ppt gives one example.
marcio
More information about the Digitalmars-d
mailing list