Skip to content

Support PHP 7.4 arrow functions (fn($x) => expr) #291

Closed
@TysonAndre

Description

@TysonAndre

These have similar properties to closures, except:

  • There is a single expression instead of a function body
  • There is no use clause, it's implicit

More details can be found in https://wiki.php.net/rfc/arrow_functions_v2

Arrow functions have lowest precedence. This means that the expression to the right of => will be consumed as far as possible:

Examples:

fn(array $x) => $x;
fn(): int => $x;
fn($x = 42) => yield $x;
fn(&$x) => $x;
fn&($x) => $x;
fn($x, ...$rest) => $rest;
static fn() => 1;

Ideas on how this would be represented:

Create a new ArrowFunctionCreationExpression with staticModifier, FunctionHeader, FunctionReturnType, arrowToken, and resultExpression: Token|Node (instead of FunctionBody). There would be no FunctionUseClause.

It would have these properties in this order (when the ast is valid):

  • staticModifier: Token|null
  • functionKeyword: Token (to reuse FunctionName, but fn instead of function)
  • byRefToken: Token|null
  • openParen: Token
  • parameters: DelimitedList\ParameterDeclarationList
  • closeParen: Token
  • colonToken: Token|null
  • questionToken: Token|null
  • returnType: Token|qualifiedName
  • arrowToken: Token (=>)
  • resultExpression: Token|Node

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions