Generalizable Function Modifier Syntax

Kevin Smith zenparsing at gmail.com
Thu Mar 13 08:17:56 PDT 2014


While working on async functions, it has become apparent to me that we are
missing a generalizable syntax for function modifiers.  The current
precedent for function modifiers is generators:

    function* ...
    { *name() {} }

Unfortunately, this precedent requires that all modifiers be punctuators.
 Sticking to that precedent would result in code whose meaning is
obfuscated behind overloaded symbol-definitions, and would therefore be
user-hostile, particularly for less experienced users.

We can get away with it for generators, because "*" is understood to mean
"a sequence of zero or more" in a wide variety of programming contexts.  In
the case of async functions, we are, I think, not so lucky.

With that in mind, I'd like to present a modest proposal for generalizable
function modifiers.

In the case of function expressions, function declarations, and methods,
the choice is pretty clear:

    modifier [NoNewLine] function ...
    { modifier name() {} }

e.g.

    async function af() {}
    { async af() { } }

The hard part is arrow functions.  One possibility would be to use another
cover grammar and place the modifier to the left of the arrow function:

    modifier [NoNewLine] Identifier =>
    modifier [NoNewLine] ArgumentList =>

We could probably reuse some of the existing arrow function parsing
strategy to back up the input stream or transform the AST.

Another possibility would be to place the modifier to the right of the
argument list:

    Identifier [NoNewLine] modifier => ...
    ArgumentList [NoNewLine] modifier => ...

e.g.

    x async=> {}
    (a, b, c) async=> {}

This would be easier to parse, and would align with a potential "generator
arrow":

    x *=> {}

(Presuming that we can give up on arrows with empty parameter lists.)

Any thoughts?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140313/31873222/attachment-0001.html>


More information about the es-discuss mailing list