{"record":{"id":"9cf5fa2566b972c9","repo":"louthy/language-ext","slug":"must-be-of-type-temperature","errorCode":null,"errorMessage":"must be of type Temperature","messagePattern":"must be of type Temperature","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Units of Measure/Temperature.cs","lineNumber":145,"sourceCode":"                              UnitType.F => Math.Abs(FtoC(rhs.Value) - Value) < epsilon,\n                              _          => throw new NotSupportedException(Type.ToString())\n                          },\n            UnitType.F => rhs.Type switch\n                          {\n                              UnitType.K => Math.Abs(KtoF(rhs.Value) - Value) < epsilon,\n                              UnitType.C => Math.Abs(CtoF(rhs.Value) - Value) < epsilon,\n                              UnitType.F => Math.Abs(rhs.Value       - Value) < epsilon,\n                              _          => throw new NotSupportedException(Type.ToString())\n                          },\n            _ => throw new NotSupportedException(Type.ToString())\n        };\n\n    public int CompareTo(object? obj) =>\n        obj switch\n        {\n            null              => 1,\n            Temperature other => CompareTo(other),\n            _                 => throw new ArgumentException($\"must be of type {nameof(Temperature)}\")\n        };\n\n    public int CompareTo(Temperature rhs) =>\n        Type switch\n        {\n            UnitType.K => rhs.Type switch\n                          {\n                              UnitType.K => Value.CompareTo(rhs.Value),\n                              UnitType.C => Value.CompareTo(CtoK(rhs.Value)),\n                              UnitType.F => Value.CompareTo(FtoK(rhs.Value)),\n                              _          => throw new NotSupportedException(Type.ToString())\n                          },\n            UnitType.C => rhs.Type switch\n                          {\n                              UnitType.K => Value.CompareTo(KtoC(rhs.Value)),\n                              UnitType.C => Value.CompareTo(rhs.Value),\n                              UnitType.F => Value.CompareTo(FtoC(rhs.Value)),\n                              _          => throw new NotSupportedException(Type.ToString())","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Units of Measure/Temperature.cs#L127-L163","documentation":"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.","triggerScenarios":"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.","commonSituations":"Legacy non-generic collections (ArrayList, Hashtable) holding mixed types; reflection-based comparison helpers; interop code comparing boxed values.","solutions":["Only compare Temperature instances with other Temperature instances; use the generic IComparable<Temperature> path or the <, >, <=, >= operators.","Before calling CompareTo(object), check `obj is Temperature` and handle other types explicitly.","Migrate non-generic collections (ArrayList) to List<Temperature> or SortedSet<Temperature>."],"exampleFix":"// before\nint r = ((IComparable)temp).CompareTo(25.0); // ArgumentException\n// after\nint r = temp.CompareTo(Temperature.FromCelcius(25.0));","handlingStrategy":"type-guard","validationCode":"if (obj is Temperature other)\n    int r = temp.CompareTo(other);\nelse\n    throw new ArgumentException($\"Expected Temperature, got {obj?.GetType().Name ?? \"null\"}\");","typeGuard":"static bool CanCompare(Temperature t, object? obj) => obj is Temperature;","tryCatchPattern":"try { r = ((IComparable)temp).CompareTo(obj); }\ncatch (ArgumentException) { /* handle non-Temperature argument: log and skip or convert */ }","preventionTips":["Use generic comparisons: List<Temperature>.Sort or the <, > operators instead of CompareTo(object).","Replace non-generic collections (ArrayList) with generic ones.","Convert doubles to Temperature (FromCelcius etc.) before any comparison."],"tags":["invalid-argument-value","type-mismatch","icomparable","temperature"],"backgroundTag":"invalid-argument-value","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"}