📜 ⬆️ ⬇️

Language extensions RASE. Operator Overloading in ActionScript

image

The next beta of the Realaxy ActionScript Editor contains several new language extensions. We have already talked about the language Traits , which allows you to use the advantages of multiple inheritance, now turn to another, equally important opportunity - overloading and creating operators.

Download the editor here .
If you are not familiar with RASE - be sure to read the introductory article .

When writing an article in build 8144, a bug was found. If you downloaded RASE beta 10 before June 8, then be sure to download the latest version.

One of the main features added to RASE beta 10 is operator overloading.
')
This feature, like multiple inheritance, is the subject of controversy among followers of different programming paradigms.

The reasons for operator overloading are compactness and naturalness.
Compactness - a * b * c or a.multiply (b). Multiply (c) .
Naturalness - with the addition of several variables, it is easier to imagine the addition sign, and not the method call. For example, adding two variables of type Point, we expect to see “ point1 + point2 ”, and not “ point1.plus (point2) ”.
Opponents of operator overload indicate a loss of control over the code. It is not always clear where the operator is overloaded and where not. Having overloaded the operator, we change the behavior of the existing code - and there may be unexpected effects.

There is no operator overload in ActionScript (ECMA3). But they are present in the currently available ECMA4 specification. There is no overload in Java, but it is in Groovy (“Java with sugar”) and Scala. There is a possibility of overloading in C #.

We do not undertake to say how good or bad it is to overload the operators. However, there is no doubt that in some cases the use of this feature is really necessary. For example, where the code contains many mathematical operations. It will look more compact when using operators, rather than method calls. And compactness is often more control.

We finish with the theory, and move on to the practice.

In the 10th beta of the Realaxy ActionScript Editor (RASE), an overloadedOperators language appeared. It is easy to add such a language (like any other) to your project - you just need to press the key combination “ctrl + L”.

By the way, overloadelOperators RASE is the port of the language overloadedOperators from the MPS platform. We decided not to reinvent the wheel and, having studied the subject area, came to the conclusion that the solution from the MPS platform is self-sufficient and satisfies all our needs. However, we have added some "goodies" from ourselves.

We believe that this is the right way for LOPs to take the best from other languages ​​and tailor it to their needs. But more about that later, in our articles on the creation of language extensions.


We will create a project and a module for our project. In the main class of the module, add the code for testing.

image
image
image

In this code, we created two variables - p1 and p2 with type Point, and we want to get the variable p3, which is the result of adding these two points.

Next, we output to the console the values ​​of all variables: p1, p2, p3. To form the string message of the kosol we used the expression values ​​from the logging language.

Our code has an error message - "Operation can't apply to these operands". Compilation is not possible.

Now create an overload statement statement.

Import the language overloadedOperators.

image

And we add a new root “OverlodedOperatorsContainer”. Right button on the package and choose from the list.

image

Let's call it "MyOperators"

image

Inside, we see two blocks for adding declarations - “overloaded binary operators” and “custom operators declarations”. Set the cursor on the first block and press ENTER. Added new operator declaration.

image

Select the operator "+" and assign the type Point to the left and right side. Set the return value of Point (change void to Point).

Now add the code that will be executed if we use the addition operator and in the right and left parts we will have variables of type Point.

image

And it's all. We move to our class, where we wrote the text code - and we see that there is no more red code.

Run the compilation. Let's create run-configuration for our module.

image
image

If we haven't done it yet, install main-class.

image

On the console, we see the message:

image

Now we will make a more complex example and override the operations of subtraction, multiplication of division. And both on Number and on Point.

image

Imagine that we have a task to calculate a point that is left-down 50 pixels from the center of the distance between two points.

image

or even easier:

image

Everything is very simple. But let's not forget that in real life flasher it will look like this:

image

We think that the two previous illustrations themselves are an argument for using overloaded operators in flash projects.

Now we will learn to create our own operators.

Let's go to the declaration of our operators “MyOperators” and move the cursor to “custom operators declarations”. Add a declaration by pressing "Enter". Indicate the visual presentation - “~ =”

image

Our operator should check the string for regular expression matching.

We describe the behavior of our operator. Add a new overload - the usual overload declaration, as previously for Point, but in the autocomplate list, select our operator. The left operand must accept a string, and the right operand must be of type Regexp. The result of our operator will be Boolean. Add the code that performs our operator - call "test" from Regexp.

image

Create a test block of code in the Main class.

image

Simple and convenient.

In order not to get lost in the new operators, at the editor level their use is connected with the declaration through navigation. Ctrl + B or Ctrl + Click on the operator - and we go to the declaration. You can also determine that the operator is overloaded if you hover the cursor on the operator and press Ctrl.

On Mac, Ctrl changes to Command, of course.

Now we teach our operator new behavior - “commutative”.
Put the cursor and select the operation "Flip Binary Operation". The right and left side is reversed. An error has appeared in the code: the editor does not know about the operator with such operands.

image

Go to the declaration of the operator. And set the "commutative" - ​​true.
We are returning - the code has ceased to be red (sometimes you need to press F5 in order for the code to be updated).

That is, our “commutative” is, in fact, the good old rule about the amount that does not change when places are relocated.

OOP gurus recommend using this behavior carefully. There may be unexpected effects. Let's take a word and save it in memory for the future.

Everything, operator overloading in ActionScript is.

Download the project .

Source: https://habr.com/ru/post/120911/


All Articles