digitalmars.D.learn - "extending" existed class
- novice2 (27/27) Oct 18 2006 Hello.
- Jarrett Billingsley (14/23) Oct 18 2006 I don't think that ability has ever existed. Maybe you're confusing it ...
- novice2 (5/8) Oct 18 2006 Ah.. Only for arrays :(
- Jarrett Billingsley (19/24) Oct 18 2006 Yep. Some people have proposed something found in C#, where you can add...
- Lionello Lunesu (4/37) Oct 18 2006 It is, and was, only possible for arrays. But I sure wish it was
- Bill Baxter (3/44) Oct 18 2006 Argh! Why so many special case rules for arrays?!
- Bill Baxter (4/16) Oct 18 2006 By the way, where is that fun feature for arrays documented? I can't
- Hasan Aljudy (2/3) Oct 18 2006 It's not! (that's the most fun part!)
- Bill Baxter (3/7) Oct 18 2006 Must be to encourage everyone to read and participate in the news
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
"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
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
"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
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
Lionello Lunesu wrote:novice2 wrote:Argh! Why so many special case rules for arrays?! --bbHello. 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
Lionello Lunesu wrote:novice2 wrote: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/. --bbCompiler 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!)
Oct 18 2006
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
Hasan Aljudy wrote:Bill Baxter wrote:Must be to encourage everyone to read and participate in the news groups. :-)By the way, where is that fun feature for arrays documented?It's not! (that's the most fun part!)
Oct 18 2006