operator overload in as3 workarounds

Tag: actionscript-3 Author: hanufo Date: 2011-02-26

I know it's not possible to overload operators in as3, and have been using the get and set functionality (which is brilliant imho), however now I'm in the middle of creating a colour class.

What I'm trying to do is a colour class, and then subtypes rgb,hsl,hsv and so on... my problem is when I make these variables how do I make it so when I read their value they appear as a uint?

for example I want to be able to do this.

var myRGBcolour:RGB = new RGB(14,21,42);
someSprite.graphics.beginFill(myRGBcolour);     //for those of you who don't know,
                                                //this function expects a uint

How can I achieve this? I was reading earlier about valueof functions but from what I can understand, you can only use that if you say something like myRGBcolour.valueOf() but that defeats the entire purpose, I might as well just make a myRGBcolour.toUint() function.

Thank's in advance.

So you want to pass your class as an argument and have it automatically become an uint? Also please explain, what would be the disadvantage of creating and using a toUint function?
I agree with EyeSeeEm - why is calling a method on the color class defeating the whole purpose?
Sorry my question wasn't clear, I just thought there might be a cleaner way to do it than using a getter function is all. I guess there isn't one though. Thank's for everyone's help. Cheers

Best Answer

you can try to override valueOf and make specific cast:

someSprite.graphics.beginFill(0+myRGBcolour); // 0+ is similar to 0x

or if you prefer:

someSprite.graphics.beginFill(myRGBcolour+0);

This construction is used in Fingers

comments:

beautiful !! Thank's @Kasztan
BTW: I wrote an article about operator's overloading in AS3: filimanjaro.com/2012/…
thank's but it seems like your settling for "new O() + 0;" at the end anwyays...lol
No, if you're using +, you don't need 0.

Other Answer1

beginFill() takes a uint, so you gotta give it one. I think having a toUint method sounds like the best plan, or having a get property.