Walter: extend existing classes with new methods?

Don Clugston dac at nospam.com.au
Wed Sep 6 05:43:09 PDT 2006


Marcio wrote:
> Chris Nicholson-Sauls wrote:
>> Okay... maybe I'm missing something here?  
> 
>   Yes, you are.
> 
> 
>> I was pretty sure we already do have this, in a little OOP concept 
>> called inheritance?  
> 
>   Nope.
> 
>  > Given a
>> library defining 'class Foo', if I need custom behavior, I simply 
>> define a 'class MyFoo:Foo' and voila!
>>
>> Or is there some deeper concept that I'm overlooking entirely?
>>
> 
> 
>   You want String to have a new method, say asURL. Or findReplace. Or 
> whatever. Not a new String subclass. You want to add functionality to 
> String.
> 
>   You want all objects to understand dumpOn (aStream). Say you are 
> writing a persistent framework. You need to add methods to Object.
> 
>   If you ever used Smalltalk, you'd know what I mean.
> 
> marcio


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.

I think it would be possible to get most of this behaviour even from D 
.166, with a bit of hackery.



More information about the Digitalmars-d mailing list