{"record":{"id":"c1234c2c88b35725","repo":"louthy/language-ext","slug":"must-be-of-type-time","errorCode":null,"errorMessage":"must be of type Time","messagePattern":"must be of type Time","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Units of Measure/Time.cs","lineNumber":45,"sourceCode":"\n    public bool Equals(Time other) =>\n        Value.Equals(other.Value);\n\n    public bool Equals(Time other, double epsilon) =>\n        Math.Abs(other.Value - Value) < epsilon;\n\n    public override bool Equals(object? obj) =>\n        obj is Time time && Equals(time);\n\n    public override int GetHashCode() =>\n        Value.GetHashCode();\n\n    public int CompareTo(object? obj) =>\n        obj switch\n        {\n            null       => 1,\n            Time other => CompareTo(other),\n            _          => throw new ArgumentException($\"must be of type {nameof(Time)}\")\n        };\n\n    public int CompareTo(Time other) =>\n        Value.CompareTo(other.Value);\n\n    public Time Add(Time rhs) =>\n        new (Value + rhs.Value);\n\n    public Time Subtract(Time rhs) =>\n        new (Value - rhs.Value);\n\n    public Time Multiply(double rhs) =>\n        new (Value * rhs);\n\n    public Time Divide(double rhs) =>\n        new (Value / rhs);\n\n    public static Time operator *(Time lhs, double rhs) =>","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Units of Measure/Time.cs#L27-L63","documentation":"Time's explicit IComparable.CompareTo(object?) throws ArgumentException('must be of type Time') when passed a non-null object that is not a Time. This is the standard non-generic IComparable contract: null compares as 'greater than nothing' (returns 1), same type delegates to CompareTo(Time), anything else is rejected. The non-generic overload is only reached through APIs typed as object or IComparable.","triggerScenarios":"Passing a non-Time object (boxed value, different unit type like Velocity, string) to a non-generic sort/compare path such as ArrayList.BinarySearch, Array.Sort(object[]), or CompareTo((object)other) where the value is not a Time.","commonSituations":"Mixing unit types in legacy non-generic collections; reflection-based comparison code; sorting heterogeneous object arrays that were assumed homogeneous; refactor that changed an element type without updating non-generic sort calls.","solutions":["Ensure all compared values are Time instances before non-generic comparison","Prefer the generic IComparable<Time>/sort overloads (List<Time>.Sort) so type mismatches cannot occur","Check `obj is Time` before calling the non-generic CompareTo","Catch ArgumentException only at legacy-collection boundaries"],"exampleFix":"// before\nobject[] xs = { new Time(1), new TimeSq(2) };\nArray.Sort(xs); // ArgumentException: must be of type Time\n// after\nvar xs2 = new List<Time> { new Time(1), new Time(2) };\nxs2.Sort();","handlingStrategy":"type-guard","validationCode":"if (obj is not Time) throw new ArgumentException($\"expected Time, got {obj?.GetType().Name}\");","typeGuard":"static bool IsTime(object? o) => o is Time;","tryCatchPattern":"try { r = ((IComparable)time).CompareTo(obj); }\ncatch (ArgumentException) { r = -1; /* or handle type mismatch explicitly */ }","preventionTips":["Use List<Time>.Sort / generic IComparable<Time> instead of object-based APIs","Avoid ArrayList and object[] for typed unit values","Check `is Time` before non-generic comparison","Keep unit types out of heterogeneous untyped collections"],"tags":["csharp","languageext","icomparable","argument"],"backgroundTag":"type-mismatch","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}