You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Timm Friebe edited this page Mar 6, 2021
·
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.
Basic syntax
fn(param1, param2, …, paramN) => expression
fn(param1, param2, …, paramN) => { statements }
// Empty parameter lists are expressed by a pair of parenthesesfn() => expression
fn() => { statements }
// Return types can be addedfn(param1, param2, …, paramN): returnType => expression
fn(param1, param2, …, paramN): returnType => { statements }
Motivation
Arrow functions allow for shorter and more concise syntax.
As in function expressions, parameters can be typed:
$connect= fn(Uri$uri) => newHttpConnection($uri);
Capturing
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.