-
Notifications
You must be signed in to change notification settings - Fork 0
Arrow functions
Timm Friebe edited this page Jun 18, 2018
·
6 revisions
Arrow functions are shorter than function expressions and automatically capture variables from the containing scope without the need for an explicit use clause.
(param1, param2, …, paramN) ==> expression
(param1, param2, …, paramN) ==> { statements }
// Empty parameter lists are expressed by a pair of parentheses
() ==> expression
() ==> { statements }
// Parentheses are optional if there is only one parameter and no return type
param ==> expression
param ==> { statements }
// Return types can be added
(param1, param2, …, paramN): returnType ==> expression
(param1, param2, …, paramN): returnType ==> { statements }Arrow functions allow for shorter and more concise syntax.
$materials->map(function($m) { return $m->length(); });
$materials->map($m ==> $m->length());As in function expressions, parameters can be typed:
$connect= (Uri $uri) ==> new HttpConnection($uri);Unlike function expressions, variables from the enclosing scope are automatically captured. Note that all variables are captured by value. If you wish to capture by reference, you'll need to use a function expression.
- Hack lambdas: The ==> operator and introduction
- PHP RFC: Arrow functions