"extending" existed class

Lionello Lunesu lio at lunesu.remove.com
Wed Oct 18 05:55:06 PDT 2006


novice2 wrote:
> Hello.
> I have existed class (for example Stream in phobos), and i need add
> new method to it. In the past (if i correctly remember), i was able
> to do it by defining function, that accept instance of class as
> first argument, and then use it function like instance.function()
> 
> Now (DMD 1.70) i can't compile this code:
> 
> //////////////////////////////
> class A
> {
>   int method1() { return 1; }
> }
> 
> int method2(A a)
> {
>   return 2;
> }
> 
> void main()
> {
>    A a = new A;
>    int i = a.method1();
>    int k = a.method2();
> }
> //////////////////////////////
> 
> Compiler say
> 1.d(20): no property 'method2' for type '1.A'
> 
> Can anyone say, what i missed, or the better way to "extend"
> existed class without declare new class based on existed?
> Thanks.

It is, and was, only possible for arrays. But I sure wish it was 
possible for all types (classes, but int/float/etc too!)

L.



More information about the Digitalmars-d-learn mailing list