louthy/language-ext · error · ArgumentException

must be of type TimeSq

Error message

must be of type TimeSq

What it means

TimeSq's explicit CompareTo(object?) throws ArgumentException('must be of type TimeSq') when given a non-null object that isn't a TimeSq. This is the conventional IComparable non-generic guard: null -> 1, matching type -> typed comparison, otherwise throw. It fires only when comparison is dispatched through an object/IComparable-typed path.

Solutions

  1. Verify element types before non-generic sorting
  2. Use generic collections/sorts (List<TimeSq>.Sort) to get compile-time safety
  3. Guard with `obj is TimeSq` before invoking CompareTo(object)
  4. Catch ArgumentException at boundaries dealing with untyped data

Example fix

// before
var al = new ArrayList { new TimeSq(1), new Time(2) };
al.Sort(); // ArgumentException: must be of type TimeSq
// after
var list = new List<TimeSq> { new TimeSq(1), new TimeSq(2) };
list.Sort();
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj is not TimeSq) throw new ArgumentException("comparison requires TimeSq");

Type guard

static bool IsTimeSq(object? o) => o is TimeSq;

Try / catch

try { r = ((IComparable)timeSq).CompareTo(obj); }
catch (ArgumentException ex) { /* log type mismatch, skip element */ }

Prevention

When it happens

Trigger: Calling CompareTo((object)x) or using non-generic sorting (ArrayList, Array.Sort(object[])) with a non-TimeSq element — e.g. a Time, int, or other unit type — inside the collection.

Common situations: Heterogeneous legacy collections mixing unit types; refactors that renamed/replaced element types; interop with non-generic BCL sorting APIs.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of louthy/language-ext@2f0e362824 (2026-09-15). Data as JSON: /api/errors/5b83495c26b05ca3. Report an issue: GitHub.

Appendix: source

Thrown at LanguageExt.Core/Units of Measure/TimeSq.cs:38

    public bool Equals(TimeSq other) =>
        Value.Equals(other.Value);

    public bool Equals(TimeSq other, double epsilon) =>
        Math.Abs(other.Value - Value) < epsilon;

    public override bool Equals(object? obj) =>
        obj is TimeSq sq && Equals(sq);

    public override int GetHashCode() =>
        Value.GetHashCode();

    public int CompareTo(object? obj) =>
        obj switch
        {
            null         => 1,
            TimeSq other => CompareTo(other),
            _            => throw new ArgumentException($"must be of type {nameof(TimeSq)}")
        };

    public int CompareTo(TimeSq other) =>
        Value.CompareTo(other.Value);

    public TimeSq Add(TimeSq rhs) =>
        new (Value + rhs.Value);

    public TimeSq Subtract(TimeSq rhs) =>
        new (Value - rhs.Value);

    public TimeSq Multiply(double rhs) =>
        new (Value * rhs);

    public TimeSq Divide(double rhs) =>
        new (Value / rhs);

    public static TimeSq operator *(TimeSq lhs, double rhs) =>

View on GitHub (pinned to 2f0e362824)