louthy/language-ext · error · ArgumentException

must be of type Temperature

Error message

must be of type Temperature

What it means

Temperature.CompareTo(object? obj) accepts an arbitrary object; if the argument is neither null nor a Temperature, it throws ArgumentException with the message "must be of type Temperature". This is the standard IComparable non-generic contract: it refuses to order against unrelated types.

Solutions

  1. Only compare Temperature instances with other Temperature instances; use the generic IComparable<Temperature> path or the <, >, <=, >= operators.
  2. Before calling CompareTo(object), check `obj is Temperature` and handle other types explicitly.
  3. Migrate non-generic collections (ArrayList) to List<Temperature> or SortedSet<Temperature>.

Example fix

// before
int r = ((IComparable)temp).CompareTo(25.0); // ArgumentException
// after
int r = temp.CompareTo(Temperature.FromCelcius(25.0));
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj is Temperature other)
    int r = temp.CompareTo(other);
else
    throw new ArgumentException($"Expected Temperature, got {obj?.GetType().Name ?? "null"}");

Type guard

static bool CanCompare(Temperature t, object? obj) => obj is Temperature;

Try / catch

try { r = ((IComparable)temp).CompareTo(obj); }
catch (ArgumentException) { /* handle non-Temperature argument: log and skip or convert */ }

Prevention

When it happens

Trigger: Calling CompareTo((object)someNonTemperature), passing a Temperature to APIs that box it into heterogeneous IComparable collections (e.g. ArrayList.Sort, non-generic sorted structures), or mixing boxed doubles/ints with Temperature values.

Common situations: Legacy non-generic collections (ArrayList, Hashtable) holding mixed types; reflection-based comparison helpers; interop code comparing boxed values.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at LanguageExt.Core/Units of Measure/Temperature.cs:145

                              UnitType.F => Math.Abs(FtoC(rhs.Value) - Value) < epsilon,
                              _          => throw new NotSupportedException(Type.ToString())
                          },
            UnitType.F => rhs.Type switch
                          {
                              UnitType.K => Math.Abs(KtoF(rhs.Value) - Value) < epsilon,
                              UnitType.C => Math.Abs(CtoF(rhs.Value) - Value) < epsilon,
                              UnitType.F => Math.Abs(rhs.Value       - Value) < epsilon,
                              _          => throw new NotSupportedException(Type.ToString())
                          },
            _ => throw new NotSupportedException(Type.ToString())
        };

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

    public int CompareTo(Temperature rhs) =>
        Type switch
        {
            UnitType.K => rhs.Type switch
                          {
                              UnitType.K => Value.CompareTo(rhs.Value),
                              UnitType.C => Value.CompareTo(CtoK(rhs.Value)),
                              UnitType.F => Value.CompareTo(FtoK(rhs.Value)),
                              _          => throw new NotSupportedException(Type.ToString())
                          },
            UnitType.C => rhs.Type switch
                          {
                              UnitType.K => Value.CompareTo(KtoC(rhs.Value)),
                              UnitType.C => Value.CompareTo(rhs.Value),
                              UnitType.F => Value.CompareTo(FtoC(rhs.Value)),
                              _          => throw new NotSupportedException(Type.ToString())

View on GitHub (pinned to 2f0e362824)