
A couple of days ago we published a new,
tenth beta of our new IDE for flashers. This article is about the Traits language extension.
Action Script is a modern OOP language. At present, the interpretation of OOP suggests that multiple inheritance is a vicious practice. Means of the language to implement multiple inheritance is possible only through interfaces. It is right and good - to argue here is meaningless.
')
But you want it! Often, due to the inability to add functionality through inheritance, we, the developers, have to fence too many gardens.
For example, we have the class Creature. We want to teach him to talk. The “right” OOP tells us what to do with the composition. We create an ISpeaker interface. Add the speak () method to it; We implement this interface - class Speaker. Our class is inherited from ISpeaker and we add the delegate. And the Speak method redirects calls to the speak () method to the deagata. Is done.
Now imagine that we have multiple inheritance. We will make a class Speaker. And inherit our Speaker Creature class. Everything.
And now let's imagine that our Creature should be able to do a lot: walk, move arms and legs, eat, smile, cry. Maybe fly. You never know what the requirements will set us the task. As a result, we have a bunch of “correct” OOP code, which does everything so verbose that it becomes difficult for even the developer to understand the real purpose of this code. And time, time.
Well, we, experienced, of course, understand that the situation described is contrived. Who will create a bunch of interfaces on every aspect of behavior? Even if these aspects need to be applied not only in Creature. No, the real code will look different - not so beautiful, not for training, but closer to life. Easier.
So it turns out that the intentions of ECMA Script developers and, later, ActionScript developers, to make a good (correct) OOP language go sideways in real daily work.
Perhaps enough complaints. We have a tool to fix it. RASE. Realaxy ActionScript Editor. This solution is a language traits - language extension ActionScript.
Immediately practice.
Let's create a new project in the editor and a module testTraits with the main-class Creature.
one.

2

3

four.

Import language traits - ctrl + L (cmd + L).

Create Speak interface. In which we create one method - “speak”.

At the bottom of the editing window we see two tabs - “Interface” and “Trait”.
Select the “Trait” tab (it is tinted gray so far) and click on the empty editor field. A dialog box appears prompting us to create a treyt.

Select “OK” and the editor created the default implementation.

Add the body of the “speak” method - just output “Hello!” To the console.

Go to the Creature class and add the “Speak” interface to the implements.

The editor added the letter “i” to the right of the interface name. This means that the interface has default implementation - it has trait behavior.

The editor knows that with this behavior, the methods are added to the class and does not highlight any errors.
Everything. Our Creature can talk! It remains to verify this in practice. Add the “speak ()” method call to the class constructor.

Add run-configuration.
one.

2

3

four.

Run our flash drive. On the console we see the message “Hello”.

What we got:
- The interface and implementation code is physically linked — bookmarks in the editor, navigation.
- The code is organized. The implementation has a name - the name of the interface plus “Impl”.
- Ease and convenience of use. In a class where this behavior is used, we just add our interface to the list of implemented.
- Separate the flies from cutlets. Creature class code is not “littered” with unnecessary entities. They said the essence of "say" and all.
We have a behavior that is indeed similar to multiple inheritance, but it also relies on the “right” OOP techniques.
Let's see how it works. What code we get in the end.
“Build-> Generate (obsolute) -> Generate Text from Current Model”. The “Output” window has appeared. We are interested in the Creature class code.

We see code that implements the classic composition. That is, the language trait extension in fact only hides from us too much and organizes code artifacts. But actually we have a clean OOP.
Now it is easy to imagine how to teach our being to any actions easily and conveniently. But the main thing is the behavior: just as never before, we can apply the same principles for other classes.
Let's do it. 10 minutes.

The Creature class can do a lot already, and the class code is also clean and not clogged.
Let's see what the code would be if we wrote it without this language extension.

On this I take my leave and wait for questions, suggestions and comments.
You can download the editor
at .
The working draft you can get
at .
If you are new to the editor, be sure to read the article “first steps” on
our website or on
Habré before the breakdown.