findKindByInstance method

Kind<T> findKindByInstance(
  1. T instance
)

Finds kind by instance.

Implementation

Kind<T> findKindByInstance(T instance) {
  Kind<T>? best;
  for (var possibleKind in defaultKinds) {
    if (possibleKind.isInstance(instance)) {
      if (best == null || best.isSubKind(possibleKind)) {
        best = possibleKind;
      }
    }
  }
  if (best == null && !isSealed) {
    for (var possibleKind in Kind.all) {
      if (possibleKind is Kind<T> && possibleKind.isInstance(instance)) {
        if (best == null || best.isSubKind(possibleKind)) {
          best = possibleKind;
        }
      }
    }
  }
  if (best == null) {
    throw ArgumentError('No kind for an instance of ${instance.runtimeType}');
  }
  return best;
}