时间:2023-10-05 22:02
人气:
作者:admin
Dart官方文档:https://dart.dev/language/pattern-types
重要说明:本博客基于Dart官网文档,但并不是简单的对官网进行翻译,在覆盖核心功能情况下,我会根据个人研发经验,加入自己的一些扩展问题和场景验证。
和操作符一样,模式运算也遵循一定的优先级规则,我们可以通过增加括号()让低优先级规则的模式优先运算:
逻辑或 < 逻辑与 < 关系。模式语法:子模式1 || 子模式2
模式规则:逻辑或模式通过||分割子模式,从左到右,任何一个子模式匹配则本模式匹配,且后面的子模式不在运算。
子模式可以绑定变量,但是每个子模式绑定的变量必须相同,因为任一子模式匹配则后面的子模式不在运算。
var isPrimary = switch (color) {
Color.red || Color.yellow || Color.blue => true,
_ => false
};
模式语法:子模式1 && 子模式2
模式规则:逻辑与模式通过&&分隔子模式,从左到右,任何一个子模式未匹配则本模式未匹配,且后面的子模式不在运算。
子模式可以绑定变量,且每个子模式绑定的变量不能重叠,因为本模式匹配代表每个子模式都必须匹配运算,如果重叠则意味着变量被赋值多次。
switch ((1, 2)) {
case (var a, var b) && (var c, var d): // ...
}
模式规则:关系模式通过和给定的常量进行比较完成匹配(比较操作符:==,!=,<,>,<=,>=),true代表匹配成功。通常情况下,关系模式和逻辑与模式配合使用。
String asciiCharType(int char) {
const space = 32;
const zero = 48;
const nine = 57;
return switch (char) {
< space => 'control',
== space => 'space',
> space && < zero => 'punctuation',
>= zero && <= nine => 'digit',
_ => ''
};
}
模式语法:变量 as 类型,如:foo as String
模式规则:值转换模式允许在对象数据解构过程中进行类型转换,如果类型无法转换,则会产生错误,建议在类型转换之前,进行类型断言。
(num, Object) record = (1, 's');
var (i as int, s as String) = record;
模式语法:子模式?
模式规则:如果检测的值不为NULL,则模式匹配。它允许绑定一个变量,变量的类型是该不可为NULL值类型基类。
String? maybeString = 'nullable with base type String';
switch (maybeString) {
case var s?:
// 's' has type non-nullable String here.
}
模式语法:子模式!
模式规则:首先检测对象不为NULL,然后检测对象数据值。如果匹配的值为NULL,则会抛出错误。它常用于解构并赋值场景,且保证所赋值非NULL。
List<String?> row = ['user', null];
switch (row) {
case ['user', var name!]: // ...
// 'name' is a non-nullable string here.
}
(int?, int?) position = (2, 3);
var (x!, y!) = position;
当值为常量时,常量模式匹配,常量包括:123, null, string, math.pi, SomeClass.constant, const Thing(1, 2), const (1 + 2)等。
switch (number) {
// Matches if 1 == number.
case 1: // ...
}
我们可以通过字面常量、命名的常量等方式使用常量模式:
123, 45.56truestringsomeConstant, math.pi, double.infinityconst Point(0, 0)const [], const {1, 2}其他更多复杂的常量表达式,可以通过()包裹,并增加const前缀:const (const (1 + 2))
// List or map pattern:
case [a, b]: // ...
// List or map literal:
case const [a, b]: // ...
模式规则:变量模式一般在解构和赋值中,它匹配模式、解构对象并完成赋值,如:var bar, String str, final int _。变量的作用域为模式所在的作用域。如果变量指定了类型,那么当对象类型和值均匹配时,模式才被匹配。通配符模式是一个特殊的变量模式。
switch ((1, 2)) {
// 'var a'和'var b'是变量模式,它们值分别为`1`和`2`
case (var a, var b): // ...
// 'a'和'b'的作用域在case代码块
}
switch ((1, 2)) {
// `2`是数字类型,与'String b'不匹配,因此本模式为匹配
case (int a, String b): // ...
}
标识符模式与常量模式或变量模式类似:
var (a, b) = (1, 2);(a, b) = (3, 4);_外)case [_, var y, _]: print('The middle element is $y');模式语法:(子模式)
代码样例:如下代码,和表达式一样,增加括号()目的是提高模式的优先级。
final (x, y, z) = (true, true, false);
// 没有括号:true
x || y && z => 'matches true',
// 增加括号:false
(x || y) && z => 'matches false',
模式语法:[子模式1, 子模式2]
List列表模式首先匹配List类型,然后匹配列表元素值,并进行解构和赋值。List列表模式必须匹配整个列表模式元素,我们可以使用...占位符匹配剩余的列表元素。
// 全列表元素匹配
const a = 'a';
const b = 'b';
switch (obj) {
case [a, b]:
print('$a, $b');
}
// 占位符匹配剩余元素,且忽略
var [a, b, ..., c, d] = [1, 2, 3, 4, 5, 6, 7];
print('$a $b $c $d'); // 1 2 6 7
// 占位符当作一个子列表
var [a, b, ...rest, c, d] = [1, 2, 3, 4, 5, 6, 7];
print('$a $b $rest $c $d'); // 1 2 [3, 4, 5] 6 7
模式语法:{"key": subpattern1, someConst: subpattern2}
Map映射模式首先匹配Map类型,然后匹配元素内容,并进行解构和赋值。Map映射模式不需要匹配所有元素,忽略未被匹配到的元素。
模式语法:(subpattern1, subpattern2)或者(x: subpattern1, y: subpattern2)
Record记录模式首先匹配记录,然后解构其字段。字段数量、类型和值未匹配,则模式匹配失败。Record记录模式必须匹配所有字段。字段getter可由变量和标识符模式推导得到。
var (myString: foo, myNumber: bar) = (myString: 'string', myNumber: 1);
// Record pattern with variable subpatterns:
var (untyped: untyped, typed: int typed) = record;
var (:untyped, :int typed) = record;
switch (record) {
case (untyped: var untyped, typed: int typed): // ...
case (:var untyped, :int typed): // ...
}
// Record pattern wih null-check and null-assert subpatterns:
switch (record) {
case (checked: var checked?, asserted: var asserted!): // ...
case (:var checked?, :var asserted!): // ...
}
// Record pattern wih cast subpattern:
var (untyped: untyped as int, typed: typed as String) = record;
var (:untyped as int, :typed as String) = record;
模式语法:SomeClass(x: subpattern1, y: subpattern2)
对象模式首先对象类型和属性类型,并完成对象属性解构,调用getter方法完成赋值。如果类型不一致,则匹配失败。对象的属性名可以忽略,它可以通过变量模式和标识符模式进行推导。和Map映射模式一样,对象模式不需要匹配所有属性,忽略未被匹配到的属性。
switch (shape) {
// Matches if shape is of type Rect, and then against the properties of Rect.
case Rect(width: var w, height: var h): // ...
}
// Binds new variables x and y to the values of Point's x and y properties.
var Point(:x, :y) = Point(1, 2);
模式语法:_
_就是通配符模式,它既是变量模式也是标识符模式,但是它无变量也不赋值。它通常作为一个占位符,目的是匹配解构剩下的位置值。通配符如果带有类型,那么它仅仅进行类型检测,而忽略变量和赋值。
// 占位符
var list = [1, 2, 3];
var [_, two, _] = list;
// 类型检测
switch (record) {
case (int _, String _):
print('First field is int and second is String.');
}
我的本博客原地址:https://ntopic.cn/p/2023100501
本文作者:奔跑的蜗牛,转载请注明原文链接:https://ntopic.cn
Flutter/Dart第21天:Dart异步编程(Future/St