1. Code
  2. ActionScript

Operator Overloading in AS3 With Realaxy Editor


In the previous post about Realaxy Editor we spoke about the Traits language, which lets you take advantage of multiple inheritance. Now we turn to another noteworthy opportunity: operator overloading.

Realaxy ActionScript Editor (RASE) is available here (for Windows, Macintosh OS X, and Linux). Note: if it is your first experience with the editor, please read the RASE how-to.


One of the major features added to the RASE beta 10 is operator overloading support, allowing developers to change how the operators (*, +, etc) behave in different circumstances. This feature, as well as multiple inheritance, is an object of a holy war between ardent supporters of two different programming paradigms.

The advantages of overloading operators are obvious:

  1. Compact style. Your programs might become shorter and more readable; for example, it's much easier to use something like a*b*c than a.multiply(b).multiply(с).
  2. Code often looks more natural. Well defined operator is easier to understand and remember than the name of function a function name. For example, combining two variables of type Point you would prefer to see something like point1 + point2, but not

The real argument against operator overloading is the losing of the control over your code. It is unclear, sometimes, where an operator is overloaded, and where it is not. Adding an overloaded operator invokes changes in the behavior of existing code that might lead to some unexpected consequences.

Operator overloading is not included in ActionScript (ECMA3) even though it is proposed for Standard ECMA4. Java does not support operator overloading but both Groovy (“Java with syntactic sugar”) and Scala do. Furthermore, C#'s operators may be overloaded.

One cannot say whether it is a good or bad thing. However, it is also practically assured that sometimes it is a virtual must-have. For example, when chaining multiple mathematical operations, operator overloading really makes code much more concise, thus improving your understanding of the way it works. But enough theory — let's refer ourselves to practice.

We talk about a language extension overloadedOperators that was introduced in RASE since Beta 10 (build 8145+).

An easy way to import it (or any other language) to your project is to press Ctrl+L (Cmd+L) within Realaxy Editor.

By the way, overloadedOperators in RASE is just a port of the overloadedOperators language from the Jetbrains MPS platform. We just decided not to reinvent the wheel and after investigating the target application domain, came to the conclusion that the Jetbrains solution appears to be sufficient, and meets all our needs. We have added some small bonus features from us.

We believe, that this is the right way of the Language Oriented Programming that takes the best from other languages and customizes it for own needs. Read more about this later in our forthcoming articles on the creation of language extensions.

Step 1

Create a new project with a module inside.

Step 2

Call the new root “operators”.

Step 3

Add some code for testing to the main class.

Here we have created two variables, p1 and p2 (type Point). Now we’d like to get a third variable, p3, which would be a result of combining these two points.

Output these three values: p1, p2, p3. We used expression values from language logging in order to implement the string message for the console output.

Our code provokes an error message: “Operation can't applied to these operands”. Compiling is impossible.

Step 4

Let’s declare the operator overloading. Import com.realaxy.actionScript.overloadedOperators.

Step 5

Add a new “OverlodedOperatorsContainer” root: right-click on the package node and choose from a pop-up menu.

Step 6

Name it “MyOperators”.

Here we see two declaration blocks: “overloaded binary operators” and “custom operators declarations”.

Step 7

Move to the first block and press Enter. A new operator declaration is added.

Step 8

Choose “+” operator. Choose type Point for both sides. Set return value Point (change it from default “void” setting).

Now we can add a code that would be executed on combining Point with Point (using “+” operator).

That’s all.

The red highlight that indicated an error in our class has disappeared.

Step 9

Start compiling.

Step 10

Create run-configuration for our module.

Step 11

Assign the main class (if you had not done it earlier).

Step 12

We get the following message on the console:

Step 13

Now let’s try to complete a slightly more sophisticated task and re-define operations of subtraction, multiplication, and division. For both Number and Point.

Step 14

Imagine that you need to calculate a point that is located to the left and down by 50 pixels from the center of the distance between the two points.

This is simple:

Or even simpler:

But don’t forget that in a Flasher’s real life it would be like this:

Compare these figures. It’s good enough reason to use operator overloading in ActionScript projects.

Step 15

Now let’s learn how to create our own custom operators.

First, return to MyOperators declaration and go to custom operators declarations.

Press Enter to create a new declaration. Visual presentation would be ~=.

Step 16

Our custom operator has to check a string for matching a regular expression. To describe such operator’s behavior we add a new overloading, just like we have done it 10 minutes ago with Point. The only difference is adding ~= to autocomplete.

The left operand must pick up a String, while the right one must be RegExp. Returning value should be Boolean.

Then add some code that would be executed by our operator, calling test method from the value.

Step 17

Create a test operation in Main () to see how it works:

Fast and easy.

To avoid confusion in all these custom operators, you should memorise a single keyboard shortcut related with navigation: Ctrl-B (or Ctrl+click on an operator) will lead you to operator’s declaration.

A good hint: to make a difference between reloaded operator and a built-in one, hover a mouse on it and press Ctrl.

Of course, if you’re on a Mac, use Cmd instead of Ctrl.

Step 18

Let’s extend the behavior of our custom operator with a new feature, commutativity.

Select the operator and press Alt-Enter. Choose Flip Binary Operation from a pop-up menu. This will swap the two sides, however with an error message, because ~= operator is not yet defined for such operands.

Proceed to operators declaration. Set commutative = true. The red error highlight should disappear (sometimes you should press F5 to refresh a view).

In other words, this “commutative” setting is a good old rule that changing the order of summands doesn't change the sum. Now, our ~= operator will still work if we put the RegExp before the String.

Word of Warning

OOP gurus warn against overusing this behavior. Sometimes, it involves unexpected effects. Let’s take it on faith and remember it.

That's all there is to it. We have operator overloading in ActionScript 3.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.