记录

Last updated: ... / Reads: 36 Edit

记录需要至少 3.0 的语言版本。

记录是一种匿名的、不可变的聚合类型。与其他集合类型一样, 它们允许您将多个对象捆绑到单个对象中。与其他系列不同 类型、记录是固定大小的、异构的和类型化的。

记录是真实的价值;您可以将它们存储在变量中, 嵌套它们,将它们传入和传出函数, 并将它们存储在数据结构中,例如列表、映射和集合。

记录语法

记录表达式是以逗号分隔的命名字段或位置字段列表, 括号内为:

var record = ('first', a: 2, b: true, 'last');

记录类型批注是用括号括起来的以逗号分隔的类型列表。 您可以使用记录类型批注来定义返回类型和参数类型。 例如,以下语句是记录类型批注:(int, int)

(int, int) swap((int, int) record) {
  var (a, b) = record;
  return (b, a);
}

记录表达式和类型批注中的字段镜像 参数和参数在函数中的工作原理。 位置字段直接位于括号内:

// Record type annotation in a variable declaration:
(String, int) record;

// Initialize it with a record expression:
record = ('A string', 123);

在记录类型批注中,命名字段位于大括号分隔的内 类型和名称对的部分,在所有位置字段之后。在记录中 表达式中,名称在每个字段值之前,后面有一个冒号:

// Record type annotation in a variable declaration:
({int a, bool b}) record;

// Initialize it with a record expression:
record = (a: 123, b: true);

记录类型中命名字段的名称是 记录的类型定义或其形状。 两条带有命名字段的记录 不同的名称有不同的类型:

({int a, int b}) recordAB = (a: 1, b: 2);
({int x, int y}) recordXY = (x: 3, y: 4);

// Compile error! These records don't have the same type.
// recordAB = recordXY;

在记录类型注释中,您还可以命名位置字段,但 这些名称仅用于文档,不会影响记录的类型:

(int a, int b) recordAB = (1, 2);
(int x, int y) recordXY = (3, 4);

recordAB = recordXY; // OK.

这类似于位置参数 在函数声明或函数 typedef 中 可以有名称,但这些名称不会影响函数的签名。

有关详细信息和示例,请查看记录类型和记录相等性。

记录字段

记录字段可通过内置的 getter 访问。记录是不可变的, 所以字段没有 setter。

命名字段公开同名的 getter。位置字段显示 getter ,跳过命名字段:$

var record = ('first', a: 2, b: true, 'last');

print(record.$1); // Prints 'first'
print(record.a); // Prints 2
print(record.b); // Prints true
print(record.$2); // Prints 'last'

为了进一步简化记录字段访问, 查看 Patterns 上的页面。

记录类型

没有针对单个记录类型的类型声明。记录是结构化的 根据其字段的类型进行键入。记录的形状(其字段集, 字段的类型及其名称(如果有)唯一地决定了记录的类型。

记录中的每个字段都有自己的类型。字段类型可能不同 记录。类型系统知道每个字段的类型,无论在何处访问它 从记录:

(num, Object) pair = (42, 'a');

var first = pair.$1; // Static type `num`, runtime type `int`.
var second = pair.$2; // Static type `Object`, runtime type `String`.

考虑两个不相关的库,它们使用相同的字段集创建记录。 类型系统知道这些记录是同一类型,即使 库不是相互耦合的。

记录相等性

如果两条记录具有相同的形状(字段集),则它们相等, 并且它们对应的字段具有相同的值。 由于命名字段顺序不是记录形状的一部分,因此命名的顺序 字段不影响相等性。

例如:

(int x, int y, int z) point = (1, 2, 3);
(int r, int g, int b) color = (1, 2, 3);

print(point == color); // Prints 'true'
({int x, int y, int z}) point = (x: 1, y: 2, z: 3);
({int r, int g, int b}) color = (r: 1, g: 2, b: 3);

print(point == color); // Prints 'false'. Lint: Equals on unrelated types.

记录根据结构自动定义和方法 他们的领域。hashCode==

多次退货

记录允许函数返回捆绑在一起的多个值。 要从返回中检索记录值, 使用模式匹配将值分解为局部变量。

// Returns multiple values in a record:
(String, int) userInfo(Map<String, dynamic> json) {
  return (json['name'] as String, json['age'] as int);
}

final json = <String, dynamic>{
  'name': 'Dash',
  'age': 10,
  'color': 'blue',
};

// Destructures using a record pattern:
var (name, age) = userInfo(json);

/* Equivalent to:
  var info = userInfo(json);
  var name = info.$1;
  var age  = info.$2;
*/

您可以从没有记录的函数返回多个值, 但其他方法也有缺点。 例如,创建一个类要详细得多,而使用其他集合 类型喜欢或失去类型安全。ListMap

注意:记录的多重回波和异构类型特性使 不同类型期货的并行化,您可以在 dart:async 文档中阅读。


Comments

Make a comment