You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@royale.apache.org by Josh Tynjala <jo...@bowlerhat.dev> on 2025/07/17 22:48:36 UTC

New AS3 language feature: Arrow function syntax

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/

Re: New AS3 language feature: Arrow function syntax

Posted by Greg Dove <gr...@gmail.com>.
Hi Josh,

Functionality first, performance second is always the mantra (or at least I
think it is).
Thanks again for this, and I hope that whatever you're facing resolves or
eases as quickly as you could hope for. Whatever it is, it certainly comes
first.

Only because you asked for feedback:
The infer types default makes sense to me too.
I can't remember if we have GCC supporting ES6 or not, I had the idea that
we were targeting ES5.The bind method on Function.prototype would probably
be useful here if we can't output ES6 directly.
fyi I hope to be more 'available' from late September, perhaps I can either
help out, or work on some other Royale things after that.

-Greg


On Fri, Jul 18, 2025 at 8:43 PM Josh Tynjala <jo...@bowlerhat.dev>
wrote:

> Hi Greg,
>
> There is no warning for omitting the return type when the braces are
> omitted. The compiler infers the return type from the body expression. I
> thought about requiring the infer-types compiler option for that, but
> considering that it is all new syntax, I feel like maybe it just makes
> sense to allow it.
>
> Yes, the generated JS currently matches how I emulated arrow functions in
> SWF. I’d like to use real JS arrow functions in the future, but I figured
> that I could start with this and improve it later. I’ve been dealing with a
> family emergency, and I plan to take some personal time, so I wanted to
> commit this as a good enough starting point to gather feedback.
>
> --
> Josh Tynjala
> Bowler Hat LLC
> https://bowlerhat.dev/
>
>
> On Fri, Jul 18, 2025 at 9:37 AM Greg Dove <gr...@gmail.com> wrote:
>
> > Nice work, Josh!
> >
> > I just tried with
> > var test:Function = () => true;
> >
> > Is that supposed to be ():Boolean => true ?
> >
> > fyi if it is, I did not see a warning (I don't think)
> >
> > I assume you are simply outputting the same 'conversion' for swf in the
> js
> > output at the moment ?
> >
> >
> >
> >
> > On Fri, Jul 18, 2025 at 2:37 PM Harbs <ha...@gmail.com> wrote:
> >
> > > Awesome! :-)
> > >
> > > > On Jul 18, 2025, at 1:48 AM, Josh Tynjala <joshtynjala@bowlerhat.dev
> >
> > > wrote:
> > > >
> > > > 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/
> > >
> > >
> >
>

Re: New AS3 language feature: Arrow function syntax

Posted by Josh Tynjala <jo...@bowlerhat.dev>.
Hi Greg,

There is no warning for omitting the return type when the braces are
omitted. The compiler infers the return type from the body expression. I
thought about requiring the infer-types compiler option for that, but
considering that it is all new syntax, I feel like maybe it just makes
sense to allow it.

Yes, the generated JS currently matches how I emulated arrow functions in
SWF. I’d like to use real JS arrow functions in the future, but I figured
that I could start with this and improve it later. I’ve been dealing with a
family emergency, and I plan to take some personal time, so I wanted to
commit this as a good enough starting point to gather feedback.

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


On Fri, Jul 18, 2025 at 9:37 AM Greg Dove <gr...@gmail.com> wrote:

> Nice work, Josh!
>
> I just tried with
> var test:Function = () => true;
>
> Is that supposed to be ():Boolean => true ?
>
> fyi if it is, I did not see a warning (I don't think)
>
> I assume you are simply outputting the same 'conversion' for swf in the js
> output at the moment ?
>
>
>
>
> On Fri, Jul 18, 2025 at 2:37 PM Harbs <ha...@gmail.com> wrote:
>
> > Awesome! :-)
> >
> > > On Jul 18, 2025, at 1:48 AM, Josh Tynjala <jo...@bowlerhat.dev>
> > wrote:
> > >
> > > 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/
> >
> >
>

Re: New AS3 language feature: Arrow function syntax

Posted by Greg Dove <gr...@gmail.com>.
Nice work, Josh!

I just tried with
var test:Function = () => true;

Is that supposed to be ():Boolean => true ?

fyi if it is, I did not see a warning (I don't think)

I assume you are simply outputting the same 'conversion' for swf in the js
output at the moment ?




On Fri, Jul 18, 2025 at 2:37 PM Harbs <ha...@gmail.com> wrote:

> Awesome! :-)
>
> > On Jul 18, 2025, at 1:48 AM, Josh Tynjala <jo...@bowlerhat.dev>
> wrote:
> >
> > 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/
>
>

Re: New AS3 language feature: Arrow function syntax

Posted by Harbs <ha...@gmail.com>.
Awesome! :-)

> On Jul 18, 2025, at 1:48 AM, Josh Tynjala <jo...@bowlerhat.dev> wrote:
> 
> 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/