beAssignableTo<U> method

U? beAssignableTo<U>()

Asserts that the object is assignable to a variable of type U

Implementation

U? beAssignableTo<U>() {
  if (isReversed) {
    Execute.assertion.forCondition(subject is U).failWith(
          '$subjectLabel\n    $subject\nshould not be a subbclass of\n    `$U`\nbut does.',
        );
  } else {
    Execute.assertion.forCondition(subject is! U).failWith(
          '$subjectLabel\n    $subject\nshould be a subbclass of\n    `$U`\nbut does not',
        );
  }

  try {
    final x = subject as U;
    return x;
  } catch (e) {
    // TODO: catch exact type cast error - example with List<String/Int/etc>
    return null;
  }
}