メインコンテンツにスキップ

relational_pattern_operand_type_not_assignable

定数式 '{0}' の型は、'{2}' 演算子のパラメータ型 '{1}' に代入できません。

説明

#

リレーショナルパターン (relational pattern) のオペランドの型が、呼び出される演算子のパラメータに代入できない場合に、アナライザーはこの診断を生成します。

#

以下のコードは、リレーショナルパターン内のオペランド (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');
  }
}