生成器
当你需要延迟地生成一连串的值时,可以考虑使用 生成器函数。Dart 内置支持两种形式的生成器方法:
同步 生成器:返回一个 Iterable 对象。
异步 生成器:返回一个 Stream 对象。
通过在函数上加 sync* 关键字并将返回值类型设置为 Iterable 来实现一个 同步 生成器函数,在函数中使用 yield 语句来传递值:
Iterable<int> naturalsTo(int n) sync* {
int k = 0;
while (k < n) yield k++;
}
实现 异步 生成器函数与同步类似,只不过关键字为 async* 并且返回值为 Stream:
Stream<int> asynchronousNaturalsTo(int n) async* {
int k = 0;
while (k < n) yield k++;
}
如果生成器是递归调用的,可是使用 yield* 语句提升执行性能:
Iterable<int> naturalsDownFrom(int n) sync* {
if (n > 0) {
yield n;
yield* naturalsDownFrom(n - 1);
}
}
可调用类
通过实现类的 call()
方法,允许使用类似函数调用的方式来使用该类的实例。
所有的类都可以定义并模拟 call() 方法,这个方法与普通 函数 是一样的,支持传参和定义返回类型等。
隔离区
大多数计算机中,甚至在移动平台上,都在使用多核 CPU。为了有效利用多核性能,开发者一般使用共享内存的方式让线程并发地运行。然而,多线程共享数据通常会导致很多潜在的问题,并导致代码运行出错。
为了解决多线程带来的并发问题,Dart 使用 isolate
替代线程,所有的 Dart 代码均运行在一个 isolate 中。每一个 isolate 有它自己的堆内存以确保其状态不被其它 isolate 访问。
所有的 Dart 代码都是在一个 isolate 中运行,而非线程。每个 isolate 都有一个单独的执行线程,并且不与其他的 isolate 共享任何可变对象。
Typedefs
类型别名是引用某一类型的简便方法,因为其使用关键字 typedef,因此通常被称作 typedef。下面是一个使用 IntList 来声明和使用类型别名的例子:
typedef IntList = List<int>;
IntList il = [1, 2, 3];
类型别名可以有类型参数:
typedef ListMapper<X> = Map<X, List<X>>;
Map<String, List<String>> m1 = {}; // Verbose.
ListMapper<String> m2 = {}; // Same thing but shorter and clearer.
针对函数,在大多数情况下,我们推荐使用 内联函数类型 替代 typedefs。然而,函数的 typedefs 仍然是有用的:
typedef Compare<T> = int Function(T a, T b);
int sort(int a, int b) => a - b;
void main() {
assert(sort is Compare<int>); // True!
}
元数据
使用元数据可以为代码增加一些额外的信息。元数据注解以@
开头,其后紧跟一个编译时常量(比如 deprecated)或者调用一个常量构造函数。
Dart 中有两个注解是所有代码都可以使用的: @deprecated
、@Deprecated
和 @override
。下面是使用 @deprecated 的示例:
class Television {
/// Use [turnOn] to turn the power on instead.
('Use turnOn instead')
void activate() {
turnOn();
}
/// Turns the TV's power on.
void turnOn() {...}
// ···
}
可以自定义元数据注解。下面的示例定义了一个带有两个参数的 @todo 注解:
class Todo {
final String who;
final String what;
const Todo(this.who, this.what);
}
使用 @Todo 注解的示例:
('Dash', 'Implement this function')
void doSomething() {
print('Do something');
}
元数据可以在 library、class、typedef、type parameter、 constructor、factory、function、field、parameter 或者 variable 声明之前使用,也可以在 import 或 export 之前使用。可使用反射在运行时获取元数据信息。
注释
Dart 支持单行注释、多行注释和文档注释。
单行注释
单行注释以 // 开始。所有在 // 和该行结尾之间的内容均被编译器忽略。
void main() {
// TODO: refactor into an AbstractLlamaGreetingFactory?
print('Welcome to my Llama farm!');
}
多行注释
多行注释以 /* 开始,以 / 结尾。所有在 / 和 */ 之间的内容均被编译器忽略(不会忽略文档注释),多行注释可以嵌套。
void main() {
/*
* This is a lot of work. Consider raising chickens.
Llama larry = Llama();
larry.feed();
larry.exercise();
larry.clean();
*/
}
文档注释
文档注释可以是多行注释,也可以是单行注释,文档注释以 /// 或者 /** 开始。在连续行上使用 /// 与多行文档注释具有相同的效果。
在文档注释中,除非用中括号括起来,否则分析器会忽略所有文本。使用中括号可以引用类、方法、字段、顶级变量、函数和参数。括号中的符号会在已记录的程序元素的词法域中进行解析。
下面是一个引用其他类和成员的文档注释:
/// A domesticated South American camelid (Lama glama).
///
/// Andean cultures have used llamas as meat and pack
/// animals since pre-Hispanic times.
///
/// Just like any other animal, llamas need to eat,
/// so don't forget to [feed] them some [Food].
class Llama {
String? name;
/// Feeds your llama [food].
///
/// The typical llama eats one bale of hay per week.
void feed(Food food) {
// ...
}
/// Exercises your llama with an [activity] for
/// [timeLimit] minutes.
void exercise(Activity activity, int timeLimit) {
// ...
}
}