Dart => expr 语法说明:
Only an expression—not a statement—can appear between the arrow (=>) and the semicolon (;).
案例:
sayHello(String name) => print("Hello, $name");
这里我认为print是一个声明。
通常我们同意:
- 表达式:可以计算为一个值。
- 语句:一段可以进行逻辑处理的代码。
=>可以理解为单行函数、表达式或单行语句(Function Expression)。
最佳答案
if ...
、for ...
、switch ...
是语句。 print()
是一个函数调用,计算结果为 void
并且是一个表达式 a + b
这也是一个函数调用,如 a.operator+(b)
只是语法不同而已。
关于Dart => expr 语法只有表达式而不是语句是部分的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53827019/