www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "extending" existed class

reply novice2 <sorry noem.ail> writes:
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.
Oct 18 2006
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"novice2" <sorry noem.ail> wrote in message 
news:eh4u36$2bsc$1 digitaldaemon.com...
 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()
...
 Can anyone say, what i missed, or the better way to "extend"
 existed class without declare new class based on existed?
 Thanks.
I don't think that ability has ever existed. Maybe you're confusing it with the ability to do so with arrays? int method(int[] x) { return x.length; } void main() { int[] arr = new int[10]; int i = method(arr); int k = arr.method(); // equivalent to above }
Oct 18 2006
parent reply novice2 <sorry noem.ail> writes:
Jarrett Billingsley (kb3ctd2 yahoo.com) 2006/10/18 08:49
I don't think that ability has ever existed.  Maybe you're confusing it with
the ability to do so with arrays?
Ah.. Only for arrays :( Thank you for information. Sorry for dumb question, but are there any other ways to "extend" existed classes? Inheritance only?
Oct 18 2006
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"novice2" <sorry noem.ail> wrote in message 
news:eh5hi3$2v32$1 digitaldaemon.com...
 Ah.. Only for arrays :(
 Thank you for information.

 Sorry for dumb question, but are there any
 other ways to "extend" existed classes?
 Inheritance only?
methods to classes from outside the class, like: class C { int method() { return 10; } } int C.method2() { return 20; } C c = new C(); c.method(); c.method2(); But this is not possible in D.
Oct 18 2006
prev sibling parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
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.
Oct 18 2006
next sibling parent Bill Baxter <wbaxter gmail.com> writes:
Lionello Lunesu wrote:
 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.
Argh! Why so many special case rules for arrays?! --bb
Oct 18 2006
prev sibling parent reply Bill Baxter <wbaxter gmail.com> writes:
Lionello Lunesu wrote:
 novice2 wrote:
 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!)
By the way, where is that fun feature for arrays documented? I can't seem to find it anywhere in the D spec at http://www.digitalmars.com/d/. --bb
Oct 18 2006
parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Bill Baxter wrote:
 By the way, where is that fun feature for arrays documented?  
It's not! (that's the most fun part!)
Oct 18 2006
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Hasan Aljudy wrote:
 Bill Baxter wrote:
 By the way, where is that fun feature for arrays documented?  
It's not! (that's the most fun part!)
Must be to encourage everyone to read and participate in the news groups. :-)
Oct 18 2006