Walter: extend existing classes with new methods?

Marcio mqmnews123 at sglebs.com
Thu Sep 7 14:58:50 PDT 2006


Don Clugston wrote:
> 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). 


	Ah, ok. Interesting. So, do you mind showing me how I can have the 
scenario where I can have String now understand a new method, say 
"asURL", which I can use from my new component? It should return a URL 
object or null if it is invalid. It is a silly scenario, but adding 
functionality to String is useful. Java 1.5 just added extra API to 
String, for example. Code that uses this API won't compile in Java 1.4. 
So, if you wanted to provide a compatibility layer, you could have it 
adding this API to String. Can that be done with the approach you sent? 
It wasn't clear to me that it could. But if it can, I am curious...

	Another scenario I would be interested to see working is how I can have 
Object return false to "isWidget" but a Widget class return true. This 
allows you to get rid of
instanceof" checks and rely purely on polymorphism. In my mental model 
you want to add the isWidget method to Object. You say you can add code 
to the interface. However, Object and Widget would have 2 opposite 
implementations, so I am not sure how adding code to the 1 interface 
would help? Please help me understand that C++ hack - I am not a C++ 
person :-)

	Thanks,

marcio



More information about the Digitalmars-d mailing list