-
Notifications
You must be signed in to change notification settings - Fork 0
OverloadedOperators
OverloadedOperators is language extension that adds operators overloading behavior to ActionScript3 code.
##Use-cases
Using of operators overloading has two major advantages:
- Compact style and readability; for example, it’s much easier to use something like
a*b*c
thana.multiply(b).multiply(с)
. - More natural look of the code. Well defined operator is easier to understand and remember than a function name. For example, combining two variables of type Point will look like
point1 + point2
, instead ofpoint1.plus(point2)
.
##Declaration
operator operatorAlias ( leftPartType, rightPartType ) -> operatorType
commutative : Boolean
( left, right ) -> returnType {
return result
}
Overloaded operator declaration expression should be placed to OverloadedOperatorsContainer, that appears instantly in the package right-click menu just after the import of the language extension.
// overloading "+" operator for the Point type
operator + (Point, Point) -> Point
commutative: false
(left, right)->Point {
// this code will be executed on combining Point with Point
return new Point(left.x + right.x, left.y + right.y);
}
overloaded operators declarationName {
// place for overloaded binary operators
// place for custom operators expression
}
Overloaded operators declaration block is intended to store overloaded operators declaration expressions. It can contain custom operators declarations, as well as overloaded binary operators declaration (like “+”, “-” or “*”). Should be placed to OverlodedOperatorsContainer.
overloaded operators myOperators {
<<overloaded binary operators>>
<<custom operators declaration>>
}
custom operator operatorAlias
Custom operator expression add a visual presentation to the newly created custom operator. After the custom operator statement is filled out, the new operator appears in autocomplete. For the rest, the syntax of a custom operators declaration doesn’t significally differs from the overloaded operators declaration.
// this sample demonstrates how to create the custom operator
// that will check a string for matching a regular expression
overloaded operators MyOperators {
// overloaded operators ------------------------- /
operator ~= (String, RegExp) -> Boolean
commutative: true
(left, right)->Boolean {
return right.test(left);
}
// custom operators ----------------------------- /
custom operator ~=
}
// ...some code skipped...
public function Main()
{
if ("Any text" ~= /^Any.+$/) {
// if true, output this message
trace "text start with 'Any'";
}
commutative : Boolean
Commutativity rule switch, that determines the overloaded operator behavior. Default state is false
. Change this behavior with care. Overusing this feature may involve unexpected effects.