Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
October 18, 2006 "extending" existed class | ||||
---|---|---|---|---|
| ||||
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. |
October 18, 2006 Re: "extending" existed class | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | "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 } |
October 18, 2006 Re: "extending" existed class | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | 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.
|
October 18, 2006 Re: "extending" existed class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | 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
|
October 18, 2006 Re: "extending" existed class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | 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 |
October 18, 2006 Re: "extending" existed class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | >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?
|
October 18, 2006 Re: "extending" existed class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote:
> By the way, where is that fun feature for arrays documented?
It's not! (that's the most fun part!)
|
October 18, 2006 Re: "extending" existed class | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | "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? Yep. Some people have proposed something found in C#, where you can add 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. |
October 19, 2006 Re: "extending" existed class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hasan Aljudy | 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. :-)
|
Copyright © 1999-2021 by the D Language Foundation