digitalmars.D - Interface Return Types
- teqDruid (21/21) Aug 16 2004 Why isn't this possible? It'd be nice (actually, nearly necessary for
- Derek Parnell (44/69) Aug 16 2004 Will it suit your purposes if you change it to ...
- teqDruid (10/46) Aug 16 2004 No. It will not. I've got a class that not only implements an interfac...
- Derek (32/78) Aug 16 2004 Ah, I understand. You want to describe an interface such that it has a
- Deja Augustine (4/82) Aug 17 2004 Eheh... helps to refresh your browser to see that there are new posts to...
- Deja Augustine (16/62) Aug 17 2004 This may seem a daft question, but have you tried casting?
- Walter (4/24) Aug 16 2004 Because B is not "covariant" with I, so an instance of B cannot masquera...
- teqDruid (14/44) Aug 16 2004 Please forgive the ignorance, but I don't understand. B
- Walter (9/53) Aug 17 2004 B can be *converted* to the I type, but cannot be used *directly* as an ...
- teqDruid (6/68) Aug 17 2004 I believe I understand now. So in order for the example to work, there
- Walter (8/76) Aug 17 2004 The mechanism to cast from I to a B is there via explicit cast. It canno...
- teqDruid (7/88) Aug 17 2004 I realize that... but I'm talking about converting from B to I, since th...
- Russ Lewis (22/90) Aug 17 2004 One solution to your problem would be this implementation:
- teqDruid (6/106) Aug 17 2004 Semantically, my example works. If the compiler is going to support it,
- Russ Lewis (3/121) Aug 17 2004 I hear you. Ideally, the compiler would implement the "I.a()"
- Matthias Becker (15/17) Aug 20 2004 It worcs semantically, correct, but it doesn't work techincally. It can'...
- Agent Smith (7/8) Aug 20 2004 What does "covariant" mean?
- Walter (6/14) Aug 21 2004 That is,
Why isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but that's (obviously) very minor. John
Aug 16 2004
On Mon, 16 Aug 2004 03:45:15 -0400, teqDruid wrote:Why isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but that's (obviously) very minor. JohnWill it suit your purposes if you change it to ... I guess what you trying to do with function 'a' is to return only those members implemented in class 'B' that derive from interface 'I'. Here is my test using an expanded class to highlight multiple interfaces... // -------------- <code> import std.stdio; interface I { I a(); } interface J { void c(); } class B : I, J { I a() { return new B(); } void c(){ writef("C\n"); } } void main() { I i; J j; B b; b = new B; b.c(); i = b.a(); // j = b.a(); // cannot implicitly convert I to J // i.c(); // no property 'c' for type 'I' // j.c(); // Access Violation } // -------------- </code> -- Derek Melbourne, Australia 16/Aug/04 6:15:40 PM
Aug 16 2004
On Mon, 16 Aug 2004 18:25:54 +1000, Derek Parnell wrote:On Mon, 16 Aug 2004 03:45:15 -0400, teqDruid wrote:No. It will not. I've got a class that not only implements an interface, but adds functionality. I want whoever is using the library to use the class' extra functionality if they know it's that class, or just use the generic interface. One problem is that a lot of my code in that class calls methods that the interface provides, and methods that it doesn't. I don't see a reason why you shouldn't be able to do this... Perhaps I'll re-post this under bugs, since it'll basically stop me dead in my tracks in a week or so (since I'll run out of other things to do) JohnWhy isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but that's (obviously) very minor. JohnWill it suit your purposes if you change it to ...
Aug 16 2004
On Mon, 16 Aug 2004 14:25:13 -0400, teqDruid wrote:On Mon, 16 Aug 2004 18:25:54 +1000, Derek Parnell wrote:Ah, I understand. You want to describe an interface such that it has a member that returns an instance of any class that implements that interface.On Mon, 16 Aug 2004 03:45:15 -0400, teqDruid wrote:No. It will not. I've got a class that not only implements an interface, but adds functionality. I want whoever is using the library to use the class' extra functionality if they know it's that class, or just use the generic interface. One problem is that a lot of my code in that class calls methods that the interface provides, and methods that it doesn't.Why isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but that's (obviously) very minor. JohnWill it suit your purposes if you change it to ...I don't see a reason why you shouldn't be able to do this... Perhaps I'll re-post this under bugs, since it'll basically stop me dead in my tracks in a week or so (since I'll run out of other things to do)Yep that seems like a reasonable idea. I can't think of anyway that D can do this yet. Obviously returning 'Object' is too broad. This code works, but maybe its too untidy for you... ---------------- import std.stdio; interface I { I a(); } class B : I { I a(){return new B();} this() {writef("B\n");} void C() { writef("C\n"); } } void main() { B b; B c; b = new B; c = cast(B)b.a(); // Explicit cast! c.C(); } ------------- -- Derek Melbourne, Australia
Aug 16 2004
In article <1iqrvs0lqsir0.evfkehx7osgz$.dlg 40tude.net>, Derek says...On Mon, 16 Aug 2004 14:25:13 -0400, teqDruid wrote:Eheh... helps to refresh your browser to see that there are new posts to a topic before replying :) -DejaOn Mon, 16 Aug 2004 18:25:54 +1000, Derek Parnell wrote:Ah, I understand. You want to describe an interface such that it has a member that returns an instance of any class that implements that interface.On Mon, 16 Aug 2004 03:45:15 -0400, teqDruid wrote:No. It will not. I've got a class that not only implements an interface, but adds functionality. I want whoever is using the library to use the class' extra functionality if they know it's that class, or just use the generic interface. One problem is that a lot of my code in that class calls methods that the interface provides, and methods that it doesn't.Why isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but that's (obviously) very minor. JohnWill it suit your purposes if you change it to ...I don't see a reason why you shouldn't be able to do this... Perhaps I'll re-post this under bugs, since it'll basically stop me dead in my tracks in a week or so (since I'll run out of other things to do)Yep that seems like a reasonable idea. I can't think of anyway that D can do this yet. Obviously returning 'Object' is too broad. This code works, but maybe its too untidy for you... ---------------- import std.stdio; interface I { I a(); } class B : I { I a(){return new B();} this() {writef("B\n");} void C() { writef("C\n"); } } void main() { B b; B c; b = new B; c = cast(B)b.a(); // Explicit cast! c.C(); } ------------- -- Derek Melbourne, Australia
Aug 17 2004
In article <pan.2004.08.16.18.25.13.608059 teqdruid.com>, teqDruid says...On Mon, 16 Aug 2004 18:25:54 +1000, Derek Parnell wrote:This may seem a daft question, but have you tried casting? -DejaOn Mon, 16 Aug 2004 03:45:15 -0400, teqDruid wrote:No. It will not. I've got a class that not only implements an interface, but adds functionality. I want whoever is using the library to use the class' extra functionality if they know it's that class, or just use the generic interface. One problem is that a lot of my code in that class calls methods that the interface provides, and methods that it doesn't. I don't see a reason why you shouldn't be able to do this... Perhaps I'll re-post this under bugs, since it'll basically stop me dead in my tracks in a week or so (since I'll run out of other things to do) JohnWhy isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but that's (obviously) very minor. JohnWill it suit your purposes if you change it to ...
Aug 17 2004
Because B is not "covariant" with I, so an instance of B cannot masquerade as an instance of I without a cast. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903 teqdruid.com...Why isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but that's (obviously) very minor. John
Aug 16 2004
Please forgive the ignorance, but I don't understand. B implements I, so an instance of B can be used as the I type. If B's method a()'s signature were modified to return type I, then I would have to do the following: B b = new B(); B myB = cast(B)b.a(); Whereas if the example worked as given the following would also work: I i = new B(); I myI = i.a(); since B (obviously) implements I, so can be implicitly cast to it. Unless there is some sort of compiler limitation preventing such. Could someone explain using smaller words? John On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:Because B is not "covariant" with I, so an instance of B cannot masquerade as an instance of I without a cast. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903 teqdruid.com...Why isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but that's (obviously) very minor. John
Aug 16 2004
B can be *converted* to the I type, but cannot be used *directly* as an I type. The layouts of the vtbl[]s are different. Think of it like this: a long can be used as if it was a short, by just ignoring the upper bits. But a float cannot be used as a short, because the representations are different. But a float can be converted to a short. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.06.57.18.155076 teqdruid.com...Please forgive the ignorance, but I don't understand. B implements I, so an instance of B can be used as the I type. If B's method a()'s signature were modified to return type I, then I would have to do the following: B b = new B(); B myB = cast(B)b.a(); Whereas if the example worked as given the following would also work: I i = new B(); I myI = i.a(); since B (obviously) implements I, so can be implicitly cast to it. Unless there is some sort of compiler limitation preventing such. Could someone explain using smaller words? John On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:masqueradeBecause B is not "covariant" with I, so an instance of B cannotthat'sas an instance of I without a cast. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903 teqdruid.com...Why isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but(obviously) very minor. John
Aug 17 2004
I believe I understand now. So in order for the example to work, there would have to be a mechanism to convert B to I during an implicit cast, yes? If so, why is that mechanism not there? Thanks for the explanation John On Tue, 17 Aug 2004 00:10:56 -0700, Walter wrote:B can be *converted* to the I type, but cannot be used *directly* as an I type. The layouts of the vtbl[]s are different. Think of it like this: a long can be used as if it was a short, by just ignoring the upper bits. But a float cannot be used as a short, because the representations are different. But a float can be converted to a short. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.06.57.18.155076 teqdruid.com...Please forgive the ignorance, but I don't understand. B implements I, so an instance of B can be used as the I type. If B's method a()'s signature were modified to return type I, then I would have to do the following: B b = new B(); B myB = cast(B)b.a(); Whereas if the example worked as given the following would also work: I i = new B(); I myI = i.a(); since B (obviously) implements I, so can be implicitly cast to it. Unless there is some sort of compiler limitation preventing such. Could someone explain using smaller words? John On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:masqueradeBecause B is not "covariant" with I, so an instance of B cannotthat'sas an instance of I without a cast. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903 teqdruid.com...Why isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but(obviously) very minor. John
Aug 17 2004
The mechanism to cast from I to a B is there via explicit cast. It cannot be done implicitly, however, since not all I's can be cast to B. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.20.08.09.459888 teqdruid.com...I believe I understand now. So in order for the example to work, there would have to be a mechanism to convert B to I during an implicit cast, yes? If so, why is that mechanism not there? Thanks for the explanation John On Tue, 17 Aug 2004 00:10:56 -0700, Walter wrote:IB can be *converted* to the I type, but cannot be used *directly* as anButtype. The layouts of the vtbl[]s are different. Think of it like this: a long can be used as if it was a short, by just ignoring the upper bits.woulda float cannot be used as a short, because the representations are different. But a float can be converted to a short. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.06.57.18.155076 teqdruid.com...Please forgive the ignorance, but I don't understand. B implements I, so an instance of B can be used as the I type. If B's method a()'s signature were modified to return type I, then Iforhave to do the following: B b = new B(); B myB = cast(B)b.a(); Whereas if the example worked as given the following would also work: I i = new B(); I myI = i.a(); since B (obviously) implements I, so can be implicitly cast to it. Unless there is some sort of compiler limitation preventing such. Could someone explain using smaller words? John On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:masqueradeBecause B is not "covariant" with I, so an instance of B cannotas an instance of I without a cast. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903 teqdruid.com...Why isn't this possible? It'd be nice (actually, nearly necessarythat'swhat I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but(obviously) very minor. John
Aug 17 2004
I realize that... but I'm talking about converting from B to I, since the vtbl[]s need to be converted. B is always an I, so it works... semantically at least. If the vtbl[]s were converted during an implicit cast, one would be able to override a method and return a sub-type of the type in the original method, as I am trying to do in my example, correct? John On Tue, 17 Aug 2004 13:44:51 -0700, Walter wrote:The mechanism to cast from I to a B is there via explicit cast. It cannot be done implicitly, however, since not all I's can be cast to B. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.20.08.09.459888 teqdruid.com...I believe I understand now. So in order for the example to work, there would have to be a mechanism to convert B to I during an implicit cast, yes? If so, why is that mechanism not there? Thanks for the explanation John On Tue, 17 Aug 2004 00:10:56 -0700, Walter wrote:IB can be *converted* to the I type, but cannot be used *directly* as anButtype. The layouts of the vtbl[]s are different. Think of it like this: a long can be used as if it was a short, by just ignoring the upper bits.woulda float cannot be used as a short, because the representations are different. But a float can be converted to a short. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.06.57.18.155076 teqdruid.com...Please forgive the ignorance, but I don't understand. B implements I, so an instance of B can be used as the I type. If B's method a()'s signature were modified to return type I, then Iforhave to do the following: B b = new B(); B myB = cast(B)b.a(); Whereas if the example worked as given the following would also work: I i = new B(); I myI = i.a(); since B (obviously) implements I, so can be implicitly cast to it. Unless there is some sort of compiler limitation preventing such. Could someone explain using smaller words? John On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:masqueradeBecause B is not "covariant" with I, so an instance of B cannotas an instance of I without a cast. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903 teqdruid.com...Why isn't this possible? It'd be nice (actually, nearly necessarythat'swhat I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but(obviously) very minor. John
Aug 17 2004
But while B is always an I, an I is not always a B. Hence your base class, which only knows about I, cannot implicitly cast it to B. All you need to do is put an explicit cast to I in B.a(). "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.21.48.12.427296 teqdruid.com...I realize that... but I'm talking about converting from B to I, since the vtbl[]s need to be converted. B is always an I, so it works... semantically at least. If the vtbl[]s were converted during an implicit cast, one would be able to override a method and return a sub-type of the type in the original method, as I am trying to do in my example, correct? John On Tue, 17 Aug 2004 13:44:51 -0700, Walter wrote:cannot beThe mechanism to cast from I to a B is there via explicit cast. Itandone implicitly, however, since not all I's can be cast to B. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.20.08.09.459888 teqdruid.com...I believe I understand now. So in order for the example to work, there would have to be a mechanism to convert B to I during an implicit cast, yes? If so, why is that mechanism not there? Thanks for the explanation John On Tue, 17 Aug 2004 00:10:56 -0700, Walter wrote:B can be *converted* to the I type, but cannot be used *directly* asthis: aItype. The layouts of the vtbl[]s are different. Think of it likebits.long can be used as if it was a short, by just ignoring the upperwork:Butwoulda float cannot be used as a short, because the representations are different. But a float can be converted to a short. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.06.57.18.155076 teqdruid.com...Please forgive the ignorance, but I don't understand. B implements I, so an instance of B can be used as the I type. If B's method a()'s signature were modified to return type I, then Ihave to do the following: B b = new B(); B myB = cast(B)b.a(); Whereas if the example worked as given the following would alsoforI i = new B(); I myI = i.a(); since B (obviously) implements I, so can be implicitly cast to it. Unless there is some sort of compiler limitation preventing such. Could someone explain using smaller words? John On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:masqueradeBecause B is not "covariant" with I, so an instance of B cannotas an instance of I without a cast. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903 teqdruid.com...Why isn't this possible? It'd be nice (actually, nearly necessarythat'swhat I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but(obviously) very minor. John
Aug 17 2004
I just don't understand when an I ever has to be implicitly converted to a B. interface I { I a(); } class B : I { B a() { return new B(); } this() {} } When someone wants to use a B, they can: B b = new B(); B myNewB = b.a(); However, when someone wants to reference a B as an I, a conversion of the vtbl[]s is required, as you tell me, so upon the following implicit cast: I i = myNewB; the B's vtbl[]s are converted to make it compatible with the I type. In addition, any calls to the a() method: I myNewI = i.a(); would have to invoke the the conversion. The virtual table would just have to be modified to call some sort of wrapper function, the way I see it, although since I'm not familiar with compiler design or implementation I'm not sure. Is this not possible (or not practical for some reason)? I'm not trying to be belligerent, just trying to understand something I clearly don't grasp very well. John On Tue, 17 Aug 2004 14:55:58 -0700, Walter wrote:But while B is always an I, an I is not always a B. Hence your base class, which only knows about I, cannot implicitly cast it to B. All you need to do is put an explicit cast to I in B.a(). "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.21.48.12.427296 teqdruid.com...I realize that... but I'm talking about converting from B to I, since the vtbl[]s need to be converted. B is always an I, so it works... semantically at least. If the vtbl[]s were converted during an implicit cast, one would be able to override a method and return a sub-type of the type in the original method, as I am trying to do in my example, correct? John On Tue, 17 Aug 2004 13:44:51 -0700, Walter wrote:cannot beThe mechanism to cast from I to a B is there via explicit cast. Itandone implicitly, however, since not all I's can be cast to B. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.20.08.09.459888 teqdruid.com...I believe I understand now. So in order for the example to work, there would have to be a mechanism to convert B to I during an implicit cast, yes? If so, why is that mechanism not there? Thanks for the explanation John On Tue, 17 Aug 2004 00:10:56 -0700, Walter wrote:B can be *converted* to the I type, but cannot be used *directly* asthis: aItype. The layouts of the vtbl[]s are different. Think of it likebits.long can be used as if it was a short, by just ignoring the upperwork:Butwoulda float cannot be used as a short, because the representations are different. But a float can be converted to a short. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.06.57.18.155076 teqdruid.com...Please forgive the ignorance, but I don't understand. B implements I, so an instance of B can be used as the I type. If B's method a()'s signature were modified to return type I, then Ihave to do the following: B b = new B(); B myB = cast(B)b.a(); Whereas if the example worked as given the following would alsoforI i = new B(); I myI = i.a(); since B (obviously) implements I, so can be implicitly cast to it. Unless there is some sort of compiler limitation preventing such. Could someone explain using smaller words? John On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:masqueradeBecause B is not "covariant" with I, so an instance of B cannotas an instance of I without a cast. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903 teqdruid.com...Why isn't this possible? It'd be nice (actually, nearly necessarythat'swhat I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface", but(obviously) very minor. John
Aug 18 2004
Consider the following code added to your project: void bar() { B b = new B(); foo(b); } I foo(I i) { return i.a(); } The call to i.a() is calling B.a(), which returns a B. But foo() knows nothing at all about B, it has never heard of B. It doesn't even know that it received a B rather than an I. There's just no way it can somehow determine that it has a B that needs converting to an I. Therefore, it is B.a() that needs to do the conversion to I. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.18.07.31.02.236327 teqdruid.com...I just don't understand when an I ever has to be implicitly converted to a B. interface I { I a(); } class B : I { B a() { return new B(); } this() {} } When someone wants to use a B, they can: B b = new B(); B myNewB = b.a(); However, when someone wants to reference a B as an I, a conversion of the vtbl[]s is required, as you tell me, so upon the following implicit cast: I i = myNewB; the B's vtbl[]s are converted to make it compatible with the I type. In addition, any calls to the a() method: I myNewI = i.a(); would have to invoke the the conversion. The virtual table would just have to be modified to call some sort of wrapper function, the way I see it, although since I'm not familiar with compiler design or implementation I'm not sure. Is this not possible (or not practical for some reason)? I'm not trying to be belligerent, just trying to understand something I clearly don't grasp very well. John On Tue, 17 Aug 2004 14:55:58 -0700, Walter wrote:class,But while B is always an I, an I is not always a B. Hence your baseto dowhich only knows about I, cannot implicitly cast it to B. All you needtheis put an explicit cast to I in B.a(). "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.21.48.12.427296 teqdruid.com...I realize that... but I'm talking about converting from B to I, sinceimplicitvtbl[]s need to be converted. B is always an I, so it works... semantically at least. If the vtbl[]s were converted during anthecast, one would be able to override a method and return a sub-type ofcorrect?type in the original method, as I am trying to do in my example,thereJohn On Tue, 17 Aug 2004 13:44:51 -0700, Walter wrote:cannot beThe mechanism to cast from I to a B is there via explicit cast. Itdone implicitly, however, since not all I's can be cast to B. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.20.08.09.459888 teqdruid.com...I believe I understand now. So in order for the example to work,cast,would have to be a mechanism to convert B to I during an implicitasyes? If so, why is that mechanism not there? Thanks for the explanation John On Tue, 17 Aug 2004 00:10:56 -0700, Walter wrote:B can be *converted* to the I type, but cannot be used *directly*then Ianthis: aItype. The layouts of the vtbl[]s are different. Think of it likebits.long can be used as if it was a short, by just ignoring the upperButa float cannot be used as a short, because the representations are different. But a float can be converted to a short. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.06.57.18.155076 teqdruid.com...Please forgive the ignorance, but I don't understand. B implements I, so an instance of B can be used as the I type. If B's method a()'s signature were modified to return type I,it.work:wouldhave to do the following: B b = new B(); B myB = cast(B)b.a(); Whereas if the example worked as given the following would alsoI i = new B(); I myI = i.a(); since B (obviously) implements I, so can be implicitly cast tonecessaryUnless there is some sort of compiler limitation preventing such. Could someone explain using smaller words? John On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:masqueradeBecause B is not "covariant" with I, so an instance of B cannotas an instance of I without a cast. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903 teqdruid.com...Why isn't this possible? It'd be nice (actually, nearlybutforwhat I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} } Also, the error message: class B 1interface function I.a is not implemented Would be more legible if it was "interface" not "linterface",that's(obviously) very minor. John
Aug 19 2004
One solution to your problem would be this implementation: class B : I [ B a_asB() { return new B(); } I a() { return a_asB(); } } This would mean that you implement the correct I interface, but functions which know about your class can get access to B directly without having to cast it back. Even nicer would be if you could do this: WARNING: THIS CODE DOESN'T WORK: class B : I { B a() { return new B(); } I I.a() { return a(); } } Walter, what are your thoughts on the above syntax as a feature for 2.0? teqDruid wrote:I believe I understand now. So in order for the example to work, there would have to be a mechanism to convert B to I during an implicit cast, yes? If so, why is that mechanism not there? Thanks for the explanation John On Tue, 17 Aug 2004 00:10:56 -0700, Walter wrote:B can be *converted* to the I type, but cannot be used *directly* as an I type. The layouts of the vtbl[]s are different. Think of it like this: a long can be used as if it was a short, by just ignoring the upper bits. But a float cannot be used as a short, because the representations are different. But a float can be converted to a short. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.06.57.18.155076 teqdruid.com...Please forgive the ignorance, but I don't understand. B implements I, so an instance of B can be used as the I type. If B's method a()'s signature were modified to return type I, then I would have to do the following: B b = new B(); B myB = cast(B)b.a(); Whereas if the example worked as given the following would also work: I i = new B(); I myI = i.a(); since B (obviously) implements I, so can be implicitly cast to it. Unless there is some sort of compiler limitation preventing such. Could someone explain using smaller words? John On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:masqueradeBecause B is not "covariant" with I, so an instance of B cannotas an instance of I without a cast. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903 teqdruid.com...Why isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} }
Aug 17 2004
Semantically, my example works. If the compiler is going to support it, there's no reason for a difference syntax. There are ways for me to do what I'm trying to do, such as your example, and casting, but both methods are... (and I don't believe there's any other way to say this) yuckie. On Tue, 17 Aug 2004 15:01:54 -0700, Russ Lewis wrote:One solution to your problem would be this implementation: class B : I [ B a_asB() { return new B(); } I a() { return a_asB(); } } This would mean that you implement the correct I interface, but functions which know about your class can get access to B directly without having to cast it back. Even nicer would be if you could do this: WARNING: THIS CODE DOESN'T WORK: class B : I { B a() { return new B(); } I I.a() { return a(); } } Walter, what are your thoughts on the above syntax as a feature for 2.0? teqDruid wrote:I believe I understand now. So in order for the example to work, there would have to be a mechanism to convert B to I during an implicit cast, yes? If so, why is that mechanism not there? Thanks for the explanation John On Tue, 17 Aug 2004 00:10:56 -0700, Walter wrote:B can be *converted* to the I type, but cannot be used *directly* as an I type. The layouts of the vtbl[]s are different. Think of it like this: a long can be used as if it was a short, by just ignoring the upper bits. But a float cannot be used as a short, because the representations are different. But a float can be converted to a short. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.06.57.18.155076 teqdruid.com...Please forgive the ignorance, but I don't understand. B implements I, so an instance of B can be used as the I type. If B's method a()'s signature were modified to return type I, then I would have to do the following: B b = new B(); B myB = cast(B)b.a(); Whereas if the example worked as given the following would also work: I i = new B(); I myI = i.a(); since B (obviously) implements I, so can be implicitly cast to it. Unless there is some sort of compiler limitation preventing such. Could someone explain using smaller words? John On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:masqueradeBecause B is not "covariant" with I, so an instance of B cannotas an instance of I without a cast. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903 teqdruid.com...Why isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} }
Aug 17 2004
I hear you. Ideally, the compiler would implement the "I.a()" definition for you. teqDruid wrote:Semantically, my example works. If the compiler is going to support it, there's no reason for a difference syntax. There are ways for me to do what I'm trying to do, such as your example, and casting, but both methods are... (and I don't believe there's any other way to say this) yuckie. On Tue, 17 Aug 2004 15:01:54 -0700, Russ Lewis wrote:One solution to your problem would be this implementation: class B : I [ B a_asB() { return new B(); } I a() { return a_asB(); } } This would mean that you implement the correct I interface, but functions which know about your class can get access to B directly without having to cast it back. Even nicer would be if you could do this: WARNING: THIS CODE DOESN'T WORK: class B : I { B a() { return new B(); } I I.a() { return a(); } } Walter, what are your thoughts on the above syntax as a feature for 2.0? teqDruid wrote:I believe I understand now. So in order for the example to work, there would have to be a mechanism to convert B to I during an implicit cast, yes? If so, why is that mechanism not there? Thanks for the explanation John On Tue, 17 Aug 2004 00:10:56 -0700, Walter wrote:B can be *converted* to the I type, but cannot be used *directly* as an I type. The layouts of the vtbl[]s are different. Think of it like this: a long can be used as if it was a short, by just ignoring the upper bits. But a float cannot be used as a short, because the representations are different. But a float can be converted to a short. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.17.06.57.18.155076 teqdruid.com...Please forgive the ignorance, but I don't understand. B implements I, so an instance of B can be used as the I type. If B's method a()'s signature were modified to return type I, then I would have to do the following: B b = new B(); B myB = cast(B)b.a(); Whereas if the example worked as given the following would also work: I i = new B(); I myI = i.a(); since B (obviously) implements I, so can be implicitly cast to it. Unless there is some sort of compiler limitation preventing such. Could someone explain using smaller words? John On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:masqueradeBecause B is not "covariant" with I, so an instance of B cannotas an instance of I without a cast. "teqDruid" <me teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903 teqdruid.com...Why isn't this possible? It'd be nice (actually, nearly necessary for what I'm doing) interface I { I a(); } class B : I { B a() { return new B(); } this() {} }
Aug 17 2004
Semantically, my example works. If the compiler is going to support it, there's no reason for a difference syntax.It worcs semantically, correct, but it doesn't work techincally. It can't I and B aren't covariant!!! It's the way intefaces work, why it can't be done. If you have a base and a derived class they are covariant. There layout is the same. No conversation has to be performed. But class implementing interfaces aren't covariant to these intefaces. An implicit cast has to be perforemd. While you normaly can't see this, it is still there. class Foo { Foo m () {return new Foo();} } class Bar { Bar m () {return new Bar();} } Bar bar = new Bar(); Foo baz = bar.m(); // no technical conversation -- Matthias Becker
Aug 20 2004
In article <cg4n3m$71g$1 digitaldaemon.com>, Matthias Becker says...I and B aren't covariant!!!What does "covariant" mean? Does "covariant" have anything to do with "invariant"? ^^ ^^ The prefix "co-" (as in "co-worker", "co-operation") implies symmetry. That is, I would expect that A is covariant with B if and only if B is covariant with A. Would that be a reasonable assumption?
Aug 20 2004
"Agent Smith" <Agent_member pathlink.com> wrote in message news:cg4o6e$7gg$1 digitaldaemon.com...In article <cg4n3m$71g$1 digitaldaemon.com>, Matthias Becker says...That is,I and B aren't covariant!!!What does "covariant" mean? Does "covariant" have anything to do with "invariant"? ^^ ^^ The prefix "co-" (as in "co-worker", "co-operation") implies symmetry.I would expect that A is covariant with B if and only if B is covariantwith A.Would that be a reasonable assumption?No. If "A" is a subset of "B", that doesn't imply that "B" is a subset of "A".
Aug 21 2004