relational_pattern_operand_type_not_assignable
常量表达式类型“{0}”无法赋值给“{2}”运算符的参数类型“{1}”。
描述
#当关系模式的操作数类型无法赋值给将被调用的运算符的参数类型时,分析器会生成此诊断信息。
示例
#以下代码会产生此诊断信息,因为关系模式中的操作数 (0
) 是 int
类型,但类 C
中定义的 >
运算符需要 C
类型的对象。
dart
class C {
const C();
bool operator >(C other) => true;
}
void f(C c) {
switch (c) {
case > 0:
print('positive');
}
}
常见修复方法
#如果 switch 正在使用正确的值,则更改 case 以将该值与正确类型的对象进行比较。
dart
class C {
const C();
bool operator >(C other) => true;
}
void f(C c) {
switch (c) {
case > const C():
print('positive');
}
}
如果 switch 正在使用错误的值,则更改用于计算匹配值的表达式。
dart
class C {
const C();
bool operator >(C other) => true;
int get toInt => 0;
}
void f(C c) {
switch (c.toInt) {
case > 0:
print('positive');
}
}