Hey folks,

I just wanted to highlight a new AS3 language feature that I have recently
implemented in the Royale compiler: arrow function expressions!

If you're not familiar, these were added to JavaScript a while back. Here
are the MDN docs for the JS version:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Basically, they have two main advantages.

1. They have shorter syntax.
2. They can use the `this` variable from the enclosing function's scope.

Here's a simple example:

var func:Function = (name:String) => "Hello, " + name;
func("Royale");

- No function keyword (uses => instead)
- Optional return type (it may be inferred from the return value)
- Optional braces around the function body, if it contains a single
expression
- If braces are omitted, no need for a `return` keyword

You could rewrite the same arrow function like this, without everything
omitted:

var func:Function = (name:String):String => {
    return "Hello, " + name;
}

Technically, this is valid too:

var func:Function = name => "Hello, " + name;

However, it will report a warning because the name parameter type is
missing, so that's not recommended.

--
Josh Tynjala
Bowler Hat LLC
https://bowlerhat.dev/

Reply via email to