louthy/language-ext · error · ArgumentException

must be of type Area

Error message

must be of type Area

What it means

Area.CompareTo(object?) throws ArgumentException with message "must be of type Area" when the boxed object passed to the non-generic IComparable.CompareTo is neither null nor an Area instance. The unit-of-measure structs implement IComparable by pattern-matching on the runtime type and reject anything else. It signals a type error at the comparison site, not a value problem.

Solutions

  1. Pass an Area (or null) to CompareTo; compare only same-unit values.
  2. Use the generic IComparable<Area>/CompareTo(Area) path or OrderBy(x => x.Value) instead of non-generic object comparison.
  3. Type-check before comparing: if (obj is Area other) ... else skip/throw your own clearer error.
  4. Store Area values in generic collections (List<Area>) so the type-safe comparer is used.

Example fix

// before
int r = ((IComparable)area).CompareTo(boxedLength); // ArgumentException
// after
int r = area.CompareTo((Area)boxedArea); // or area.Value.CompareTo(other.Value)
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj is null || obj is Area) { /* safe to compare */ }

Type guard

static bool IsComparableArea(object? obj) => obj is null or Area;

Try / catch

try { r = ((IComparable)area).CompareTo(obj); }
catch (ArgumentException ex) when (ex.Message.Contains("must be of type Area")) { /* handle mismatch */ }

Prevention

When it happens

Trigger: Calling CompareTo(object) on an Area with any non-Area, non-null object, e.g. area.CompareTo(5), area.CompareTo(someLength), or comparing Area against boxed values via non-generic sorting APIs (ArrayList.Sort, non-generic IComparer paths).

Common situations: Legacy non-generic collection sorting that stores mixed object types; dynamically loaded data (reflection, deserialization) where the compile-time Area type was lost; mixing LanguageExt unit types (Area vs Length vs Mass) that share a similar shape.

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/0bad4a7a3ac53869. Report an issue: GitHub.

Appendix: source

Thrown at LanguageExt.Core/Units of Measure/Area.cs:44

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

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

    public override bool Equals(object? obj) =>
        obj is Area area && Equals(area);

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

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

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

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

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

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

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

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

View on GitHub (pinned to 2f0e362824)