Official Dart docs is sometimes too simple to provide ultimate answers for various language questions. I discoverd an alternative syntax for named lambda here and here. In Dart,
(args) => expr
is the shorthand for
(args) {
return expr
}
So the Lambda-style (C++11) names variable by assigning it to a messy functor type (inferred with var in Dart and auto in C++11):
var f = (args) => expr
Which can also be rewritten using C-style function pointer-like declaration except
- the body
expris included (which is not allowed in C), returnmust be explicit in curly braces block.- arrow notation
=>takes whatever the expr evaluates to (which can be null for statements like print) - it’s simply the function name
fin Dart instead of pointer(*f)in C
[optional return type] f(args) {
return expr;
}
which can be simplified with the arrow operator
[optional return type] f(args) => { expr }
![]()