isSubtypeOf method

  1. @override
bool isSubtypeOf(
  1. RuntimeType other, {
  2. Object? value,
})
override

Checks if this class is a subtype of the other class. This considers inheritance (extends), mixin application (with), interface implementation (implements), and bridged superclass chains.

Implementation

@override
bool isSubtypeOf(RuntimeType other, {Object? value}) {
  if (other is InterpretedClass) {
    // Check for identity (reflexive)
    if (this == other) return true;

    // Check direct inheritance (superclass chain)
    InterpretedClass? current = superclass;
    while (current != null) {
      if (current == other) return true;
      current = current.superclass;
    }

    // Check applied mixins (recursively)
    for (final mixin in mixins) {
      if (mixin.isSubtypeOf(other)) return true;
    }

    // Check implemented interfaces (recursively)
    for (final interface in interfaces) {
      if (interface.isSubtypeOf(other)) return true;
    }

    // Check superclass's mixins and interfaces (transitive)
    if (superclass != null) {
      if (superclass!.isSubtypeOf(other)) return true;
    }

    // Default case: not a subtype
    return false;
  }

  // RC-7: Check bridged superclass chain when comparing against a BridgedClass.
  // An interpreted class extending a bridged class (e.g., MyWidget extends
  // StatelessWidget) must be recognized as a subtype of Widget, etc.
  if (other is BridgedClass) {
    // Walk up the interpreted superclass chain checking bridged superclasses
    InterpretedClass? current = this;
    while (current != null) {
      final bridgedSuper = current.bridgedSuperclass;
      if (bridgedSuper != null) {
        // Check if the bridged superclass matches or is a subtype of other
        if (bridgedSuper.name == other.name) return true;
        if (bridgedSuper.isSubtypeOf(other)) return true;
      }

      // Also check bridged mixins at each level
      for (final bridgedMixin in current.bridgedMixins) {
        if (bridgedMixin.name == other.name) return true;
        if (bridgedMixin.isSubtypeOf(other)) return true;
      }

      current = current.superclass;
    }

    // Check interpreted mixins' bridged superclass chains
    for (final mixin in mixins) {
      if (mixin.isSubtypeOf(other)) return true;
    }
  }

  return false;
}