{"record":{"id":"0bad4a7a3ac53869","repo":"louthy/language-ext","slug":"must-be-of-type-area","errorCode":null,"errorMessage":"must be of type Area","messagePattern":"must be of type Area","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Units of Measure/Area.cs","lineNumber":44,"sourceCode":"\n    public bool Equals(Area other) =>\n        Value.Equals(other.Value);\n\n    public bool Equals(Area other, double epsilon) =>\n        Math.Abs(other.Value - Value) < epsilon;\n\n    public override bool Equals(object? obj) =>\n        obj is Area area && Equals(area);\n\n    public override int GetHashCode() =>\n        Value.GetHashCode();\n\n    public int CompareTo(object? obj) => \n        obj switch\n        {\n            null       => 1,\n            Area other => CompareTo(other),\n            _          => throw new ArgumentException($\"must be of type {nameof(Area)}\")\n        };\n\n    public int CompareTo(Area other) =>\n        Value.CompareTo(other.Value);\n\n    public Area Add(Area rhs) =>\n        new (Value + rhs.Value);\n\n    public Area Subtract(Area rhs) =>\n        new (Value - rhs.Value);\n\n    public Area Multiply(double rhs) =>\n        new (Value * rhs);\n\n    public Area Divide(double rhs) =>\n        new (Value / rhs);\n\n    public static Area operator *(Area lhs, double rhs) =>","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Units of Measure/Area.cs#L26-L62","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Pass an Area (or null) to CompareTo; compare only same-unit values.","Use the generic IComparable<Area>/CompareTo(Area) path or OrderBy(x => x.Value) instead of non-generic object comparison.","Type-check before comparing: if (obj is Area other) ... else skip/throw your own clearer error.","Store Area values in generic collections (List<Area>) so the type-safe comparer is used."],"exampleFix":"// before\nint r = ((IComparable)area).CompareTo(boxedLength); // ArgumentException\n// after\nint r = area.CompareTo((Area)boxedArea); // or area.Value.CompareTo(other.Value)","handlingStrategy":"type-guard","validationCode":"if (obj is null || obj is Area) { /* safe to compare */ }","typeGuard":"static bool IsComparableArea(object? obj) => obj is null or Area;","tryCatchPattern":"try { r = ((IComparable)area).CompareTo(obj); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"must be of type Area\")) { /* handle mismatch */ }","preventionTips":["Always use the generic CompareTo(Area) overload or typed collections","Avoid non-generic sorting APIs (ArrayList) for unit-of-measure types","Convert other units explicitly before comparing"],"tags":["argument-exception","units-of-measure","comparison","type-safety"],"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"}