Classes and Operators

I’m certainly no expert when it comes to writing Flash classes, but here’s my question. Can you use operators on class members? I did a quick search on the web and only found information for C++ where it’s called “overloading operators” but I didn’t find anything about it in ActionScript. In case you’re curious why I’d like to use operators on a class, here’s what I was thinking…

Let’s say I create 2 instances of the Point class:

//
var a = new Point(1, 2);
var b = new Point(3, 4);
//
// I'd like to multiply point A by point B.
var c = a * b;
// Essentially saying:
// c = new Point (3, 8);
//
// I'd also like to multiply point A by a number.
var d = a * 6;
// Essentially saying:
// d = new Point(6, 12);
//

Of course I’d also like to be able add, subtract and divide points. And while I’m at it, being able to use assignment operators such as +=, -=, /= and *= would be nice too.

Is this something that is supported in ActionScript 3? If not, do you think it will be? Do you think it should be?

Leave a comment!

4 Responses to “Classes and Operators”

  1. Pixelwit says:

    Hi Rezmason, thanks for the comment. While I don’t know a thing about operator overloading in other languages, I agree that it would probably be difficult to implement in AS, which is why I would like Adobe to handle the whole process for me.

    They allow you to use the “+” operator to concatenate instances of the String class and they also have the “concat” method which basically does the same thing but it’s my guess that 99% of all ActionScripters use “+” rather than “concat” because it makes more sense to them. I’m looking for the same sort of ~sensibility~ in the point class.

    Do you think this feature would require the EcmaScript standard to be modified?

  2. rezmason says:

    Although operator overloading would be nice, I don’t believe it will appear in EcmaScript anytime soon. And because EcmaScript directs the development of AS, that means it’ll be more than a few years before operator overloading will appear in Flash.

    But for now, I think it’s for the better. A major advantage of AS3 over languages such as C++ is that you have to invest less time toward learning the language, which reduces the learning curve. Web languages (what I call AS3 and its brethren) are an opportunity for the masses to take up programming, and language features such as operator overloading might unnecessarily complicate AS3.

    Besides, aren’t getters and setters enough? 😛 If you want to overload an operator, nothing can prevent you from creating a function such as add() or subtract().

  3. Pixelwit says:

    As I’m not an expert at writing classes or AS3, most of what you said went right over my head. 😉 I wasn’t even aware that there was a difference between a “class member” and a “class instance”. 😮

    Can you still use “+” to concatenate strings in AS3? If Adobe won’t let me override the operators, maybe there’s a chance they’ll add a new feature allowing us to use operators on to the Point class.

  4. sk says:

    Hey, at the moment there is no mention of operator overloading being supported in AS3. That’s a shame, as I agree it’d be nice to have.

    The AS3 language is really powerful compared to AS2 but there are still a few little things missing that would make it better, operator overloading being one of them!

    So using operators on an actual class instance is a no – but you did mention using operators on class members. If the particular class member is an instance of a class the closest you could get is by using the get/set methods to allow custom accessor/mutator functions for that member.

Leave a Reply

PixelWit.com's Comment Guidelines


Warning: Undefined variable $user_ID in /home2/pixelwit/public_html/blog/wp-content/themes/fvariant2/comments.php on line 57

You must be logged in to post a comment.

© Sean O'Shell 2007-2024