deep static method

  1. @Possible({StackOverflowError})
  2. @useResult
bool deep(
  1. Object? a,
  2. Object? b
)

Determines if a and b are deeply equal.

This function handles lists, maps, sets, iterables and map entries specially. A list is only equal to another list if they contain the same elements in the same order. All other iterables only need to contain the same elements to be equal.

Where possible, DeepEqualityIterable.equals and DeepEqualityMap.equals are preferred.

Contract

A StackOverflowError is thrown if a or b contain themselves.

final a = [];
a.add(a);

Equality.deep(a, ['some other list']) // Throws StackOverflowError

Implementation

@Possible({StackOverflowError})
@useResult static bool deep(Object? a, Object? b) => switch ((a, b)) {
  (_, _) when identical(a, b) => true,
  (final List a, final List b) =>  _ordered(a, b),
  (final Iterable a, final Iterable b) => _unordered(a, b),
  (Map(entries: final a), Map(entries: final b)) => _unordered(a, b),
  (final MapEntry a, final MapEntry b) => deep(a.key, b.key) && deep(a.value, b.value),
  _ => a == b,
};