Walter: extend existing classes with new methods?

Don Clugston dac at nospam.com.au
Thu Sep 7 05:12:57 PDT 2006


Marcio wrote:
> 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 much more than that. It's about extending functionality to a class 
without polluting the base class. It is not just about renaming 
interfaces (although in the link I showed, it does) -- it can be used to 
add code as well (you just need to populate the interface at compile 
time). IMHO, this kind of interface arises more frequently than 
interface inheritance does.

 >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.

Yes, but that example would work perfectly well with static typing.



More information about the Digitalmars-d mailing list