louthy/language-ext · error · ArgumentException

must be of type Accel

Error message

must be of type Accel

What it means

Accel.CompareTo(object? obj) throws ArgumentException('must be of type Accel') when the boxed object passed is neither null nor an Accel. This is the standard IComparable contract: null is ordered before the current value, same-type values are compared by Value, and any other type is rejected.

Solutions

  1. Compare against another Accel: wrap or convert the other operand to Accel before calling CompareTo.
  2. Use the generic CompareTo(Accel other) overload, which is type-safe and never throws this error.
  3. Filter or type-check heterogeneous collections before sorting them with Comparer<Accel>.Default.
  4. If you intended to compare magnitudes, extract the underlying Value (double) from both operands and compare those.

Example fix

// before
object x = 9.8; // boxed double
accel.CompareTo(x); // ArgumentException: must be of type Accel
// after
if (x is Accel other)
    accel.CompareTo(other);
else
    accel.CompareTo(new Accel(Convert.ToDouble(x)));
Defensive patterns

Strategy: type-guard

Validate before calling

// before calling the non-generic CompareTo
if (obj is not null && obj is not Accel)
    throw new ArgumentException($"Expected Accel, got {obj.GetType().Name}");

Type guard

static bool IsAccel(object? obj) => obj is Accel;

Try / catch

try
{
    result = accel.CompareTo((object)maybeAccel);
}
catch (ArgumentException)
{
    result = -1; // or handle non-Accel operand explicitly
}

Prevention

When it happens

Trigger: Passing a non-Accel object (e.g. a double, another unit-of-measure type like Vel or Length, or a boxed int) to the non-generic IComparable.CompareTo — typically via Array.Sort, List.Sort, Comparer.Default, or any API taking object.

Common situations: Sorting a mixed object[] containing several LanguageExt unit-of-measure types; comparing Accel against raw numeric values assuming implicit conversion; reflection-based comparisons that erase the static type.

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

Appendix: source

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

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

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

    public override bool Equals(object? obj) =>
        obj is Accel accel && Equals(accel);

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

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

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

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

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

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

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

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

View on GitHub (pinned to 2f0e362824)