max<T> function

  1. @Possible({TypeError})
  2. @useResult
T max<T>(
  1. T a,
  2. T b, {
  3. Select<T, Comparable<Object>>? by,
})

Returns the greater of a and b.

If by is given, the produced Comparable is used. Otherwise a and b must be Comparables.

This function is unstable, either a or b may be returned if both are equal. Unlike math.max, it works on all Comparables.

max(('a', 1), ('a', 2), by: (e) => e.$2); // ('a', 2)

max(1, 2); // 2

Implementation

@Possible({TypeError})
@useResult T max<T>(T a, T b, {Select<T, Comparable<Object>>? by}) => switch (by) {
  _? => by(a).compareTo(by(b)) > 0 ? a : b,
  _ => (a as Comparable).compareTo(b) > 0 ? a : b,
};