RE: [flexcoders] Returning 'this' from an overridden method?
Well, it really depends on the ECMA working group. I would like to get it in, and I will push for it, and I imagine that others will as well. -Sho From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Ralf Bokelberg Sent: Wednesday, February 07, 2007 11:10 AM To: flexcoders@yahoogroups.com Subject: Re: [flexcoders] Returning 'this' from an overridden method? Nice to know the name of this feature. Just lately somebody asked me the same question. He also came from a Java background. Any chance we will see it in the future? Cheers, Ralf. On 2/7/07, Sho Kuwamoto <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> > wrote: The short answer is that there is no way to do what you want for the time being. What you are looking for is a language feature called "covariant return types". In certain languages (C++, Java) you can have your subclass redefine the type that is returned by a method. This would be a great thing to get into the AS language, but it is not there now. There are further wrinkles when you consider how you would extend properties in a similar way (I wrote about the issues at http://kuwamoto.org/2007/01/22/covariant-property-types/ <http://kuwamoto.org/2007/01/22/covariant-property-types/> ). Cheers, -Sho From: flexcoders@yahoogroups.com [mailto:flexcoders@yahoogroups.com <http://yahoogroups.com> ] On Behalf Of David_Stafford Sent: Tuesday, February 06, 2007 6:13 PM To: flexcoders@yahoogroups.com Subject: [flexcoders] Returning 'this' from an overridden method? Please pardon this simple-minded question from an AS3 novice. My base class often returns 'this' from methods which makes it convenient to write code like: camera.move( 10, 12 ).rotateX( 45 ).rotateY( 90 ); The problem comes when code extends the base class and overrides one of these functions. The compiler insists, correctly, that the return type of an overriding function must match the one in the base class. What I want to do is return the 'this' object that is of the type of the derived class. Is this possible? The following is a contrived example to demonstrate. It won't compile because the overridden function in the derived class wants to return a type of MyCamera rather than Camera: public class Camera { var x:int = 0; var y:int = 0; public function move( x:int, y:int ) :Camera { this.x += x; this.y += y; return( this ); } } public class MyCamera extends Camera { override public function move( x:int, y:int ) :MyCamera { super.move( x, y ); if( x < 0 ) x = 0; if( y < 0 ) y = 0; return( this ); } } -- Ralf Bokelberg <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> > Flex & Flash Consultant based in Cologne/Germany Phone +49 (0) 221 530 15 35
RE: [flexcoders] Returning 'this' from an overridden method?
That's up to the ECMAScxript committee. But since I think an Adobe engineer chairs or co-chairs it, we have some influence. - Gordon From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Ralf Bokelberg Sent: Wednesday, February 07, 2007 11:10 AM To: flexcoders@yahoogroups.com Subject: Re: [flexcoders] Returning 'this' from an overridden method? Nice to know the name of this feature. Just lately somebody asked me the same question. He also came from a Java background. Any chance we will see it in the future? Cheers, Ralf. On 2/7/07, Sho Kuwamoto <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> > wrote: The short answer is that there is no way to do what you want for the time being. What you are looking for is a language feature called "covariant return types". In certain languages (C++, Java) you can have your subclass redefine the type that is returned by a method. This would be a great thing to get into the AS language, but it is not there now. There are further wrinkles when you consider how you would extend properties in a similar way (I wrote about the issues at http://kuwamoto.org/2007/01/22/covariant-property-types/ <http://kuwamoto.org/2007/01/22/covariant-property-types/> ). Cheers, -Sho From: flexcoders@yahoogroups.com [mailto:flexcoders@yahoogroups.com <http://yahoogroups.com> ] On Behalf Of David_Stafford Sent: Tuesday, February 06, 2007 6:13 PM To: flexcoders@yahoogroups.com Subject: [flexcoders] Returning 'this' from an overridden method? Please pardon this simple-minded question from an AS3 novice. My base class often returns 'this' from methods which makes it convenient to write code like: camera.move( 10, 12 ).rotateX( 45 ).rotateY( 90 ); The problem comes when code extends the base class and overrides one of these functions. The compiler insists, correctly, that the return type of an overriding function must match the one in the base class. What I want to do is return the 'this' object that is of the type of the derived class. Is this possible? The following is a contrived example to demonstrate. It won't compile because the overridden function in the derived class wants to return a type of MyCamera rather than Camera: public class Camera { var x:int = 0; var y:int = 0; public function move( x:int, y:int ) :Camera { this.x += x; this.y += y; return( this ); } } public class MyCamera extends Camera { override public function move( x:int, y:int ) :MyCamera { super.move( x, y ); if( x < 0 ) x = 0; if( y < 0 ) y = 0; return( this ); } } -- Ralf Bokelberg <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> > Flex & Flash Consultant based in Cologne/Germany Phone +49 (0) 221 530 15 35
Re: [flexcoders] Returning 'this' from an overridden method?
Nice to know the name of this feature. Just lately somebody asked me the same question. He also came from a Java background. Any chance we will see it in the future? Cheers, Ralf. On 2/7/07, Sho Kuwamoto <[EMAIL PROTECTED]> wrote: The short answer is that there is no way to do what you want for the time being. What you are looking for is a language feature called "covariant return types". In certain languages (C++, Java) you can have your subclass redefine the type that is returned by a method. This would be a great thing to get into the AS language, but it is not there now. There are further wrinkles when you consider how you would extend properties in a similar way (I wrote about the issues at http://kuwamoto.org/2007/01/22/covariant-property-types/). Cheers, -Sho -- *From:* flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] *On Behalf Of *David_Stafford *Sent:* Tuesday, February 06, 2007 6:13 PM *To:* flexcoders@yahoogroups.com *Subject:* [flexcoders] Returning 'this' from an overridden method? Please pardon this simple-minded question from an AS3 novice. My base class often returns 'this' from methods which makes it convenient to write code like: camera.move( 10, 12 ).rotateX( 45 ).rotateY( 90 ); The problem comes when code extends the base class and overrides one of these functions. The compiler insists, correctly, that the return type of an overriding function must match the one in the base class. What I want to do is return the 'this' object that is of the type of the derived class. Is this possible? The following is a contrived example to demonstrate. It won't compile because the overridden function in the derived class wants to return a type of MyCamera rather than Camera: public class Camera { var x:int = 0; var y:int = 0; public function move( x:int, y:int ) :Camera { this.x += x; this.y += y; return( this ); } } public class MyCamera extends Camera { override public function move( x:int, y:int ) :MyCamera { super.move( x, y ); if( x < 0 ) x = 0; if( y < 0 ) y = 0; return( this ); } } -- Ralf Bokelberg <[EMAIL PROTECTED]> Flex & Flash Consultant based in Cologne/Germany Phone +49 (0) 221 530 15 35
RE: [flexcoders] Returning 'this' from an overridden method?
The short answer is that there is no way to do what you want for the time being. What you are looking for is a language feature called "covariant return types". In certain languages (C++, Java) you can have your subclass redefine the type that is returned by a method. This would be a great thing to get into the AS language, but it is not there now. There are further wrinkles when you consider how you would extend properties in a similar way (I wrote about the issues at http://kuwamoto.org/2007/01/22/covariant-property-types/). Cheers, -Sho From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of David_Stafford Sent: Tuesday, February 06, 2007 6:13 PM To: flexcoders@yahoogroups.com Subject: [flexcoders] Returning 'this' from an overridden method? Please pardon this simple-minded question from an AS3 novice. My base class often returns 'this' from methods which makes it convenient to write code like: camera.move( 10, 12 ).rotateX( 45 ).rotateY( 90 ); The problem comes when code extends the base class and overrides one of these functions. The compiler insists, correctly, that the return type of an overriding function must match the one in the base class. What I want to do is return the 'this' object that is of the type of the derived class. Is this possible? The following is a contrived example to demonstrate. It won't compile because the overridden function in the derived class wants to return a type of MyCamera rather than Camera: public class Camera { var x:int = 0; var y:int = 0; public function move( x:int, y:int ) :Camera { this.x += x; this.y += y; return( this ); } } public class MyCamera extends Camera { override public function move( x:int, y:int ) :MyCamera { super.move( x, y ); if( x < 0 ) x = 0; if( y < 0 ) y = 0; return( this ); } }
Re: [flexcoders] Returning 'this' from an overridden method?
Hi David, I to am an AS3 novice. Wrote my first(trivial) action script file about an hour ago. :) A couple of ideas. Not sure if they will be of much help to you, but incase you hadnt considered them already.. . you could return the value returned by the call to super. ie) Camera c = super.move; //// some local x,y stuff return c; . you could use an interface as the return value. eg) interface ICamera would define the move and rotate methods and return an ICamera and your Camera classes would implement ICamera. I realise that these options do not do what you asked for, but you might find one of these is an acceptable alternative. HTH. cheers, - shaun David_Stafford wrote: > Please pardon this simple-minded question from an AS3 novice. > > My base class often returns 'this' from methods which makes it > convenient to write code like: > > camera.move( 10, 12 ).rotateX( 45 ).rotateY( 90 ); > > The problem comes when code extends the base class and overrides one > of these functions. The compiler insists, correctly, that the return > type of an overriding function must match the one in the base class. > What I want to do is return the 'this' object that is of the type of > the derived class. Is this possible? > > The following is a contrived example to demonstrate. It won't > compile because the overridden function in the derived class wants to > return a type of MyCamera rather than Camera: > > > public class Camera > { > var x:int = 0; > var y:int = 0; > > public function move( x:int, y:int ) :Camera > { > this.x += x; > this.y += y; > > return( this ); > } > } > > public class MyCamera extends Camera > { > override public function move( x:int, y:int ) :MyCamera > { > super.move( x, y ); > > if( x < 0 ) x = 0; > if( y < 0 ) y = 0; > > return( this ); > } > }
Re: [flexcoders] Returning 'this' from an overridden method?
- Original Message - From: "David_Stafford" <[EMAIL PROTECTED]> To: Sent: Wednesday, February 07, 2007 2:12 AM Subject: [flexcoders] Returning 'this' from an overridden method? > Please pardon this simple-minded question from an AS3 novice. > > My base class often returns 'this' from methods which makes it > convenient to write code like: > > camera.move( 10, 12 ).rotateX( 45 ).rotateY( 90 ); > > The problem comes when code extends the base class and overrides one > of these functions. The compiler insists, correctly, that the return > type of an overriding function must match the one in the base class. > What I want to do is return the 'this' object that is of the type of > the derived class. Is this possible? > > The following is a contrived example to demonstrate. It won't > compile because the overridden function in the derived class wants to > return a type of MyCamera rather than Camera: This is the correct behaviour. The derived class is MyCamera and any reference to this, either in the derived class or it's parent will be working with an object of class MyCamera not Camera. Make the return type Camera, then it will work. You will be returning an object reference of class Camera, but in fact it is an object reference to an object of class MyCamera. You can then cast it elsewhere as a 'MyCamera' in the application. The problem is that you are trying to change the signature of the method that you are overriding. Paul > public class Camera > { > var x:int = 0; > var y:int = 0; > > public function move( x:int, y:int ) :Camera > { > this.x += x; > this.y += y; > > return( this ); > } > } > > public class MyCamera extends Camera > { > override public function move( x:int, y:int ) :MyCamera > { > super.move( x, y ); > > if( x < 0 ) x = 0; > if( y < 0 ) y = 0; > > return( this ); > } > } > > > > > > -- > Flexcoders Mailing List > FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt > Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com > Yahoo! Groups Links > > > >
[flexcoders] Returning 'this' from an overridden method?
Please pardon this simple-minded question from an AS3 novice. My base class often returns 'this' from methods which makes it convenient to write code like: camera.move( 10, 12 ).rotateX( 45 ).rotateY( 90 ); The problem comes when code extends the base class and overrides one of these functions. The compiler insists, correctly, that the return type of an overriding function must match the one in the base class. What I want to do is return the 'this' object that is of the type of the derived class. Is this possible? The following is a contrived example to demonstrate. It won't compile because the overridden function in the derived class wants to return a type of MyCamera rather than Camera: public class Camera { var x:int = 0; var y:int = 0; public function move( x:int, y:int ) :Camera { this.x += x; this.y += y; return( this ); } } public class MyCamera extends Camera { override public function move( x:int, y:int ) :MyCamera { super.move( x, y ); if( x < 0 ) x = 0; if( y < 0 ) y = 0; return( this ); } }