{"record":{"id":"1b63ead3ea6bb60d","repo":"clockworklabs/SpacetimeDB","slug":"argument-must-be-a-i128","errorCode":null,"errorMessage":"Argument must be a I128","messagePattern":"Argument must be a I128","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"crates/bindings-csharp/BSATN.Runtime/BSATN/I128.cs","lineNumber":49,"sourceCode":"\n    internal ulong Lower => _lower;\n\n    internal ulong Upper => _upper;\n\n    /// <inheritdoc cref=\"IComparable.CompareTo(object)\" />\n    public int CompareTo(object? value)\n    {\n        if (value is I128 other)\n        {\n            return CompareTo(other);\n        }\n        else if (value is null)\n        {\n            return 1;\n        }\n        else\n        {\n            throw new ArgumentException(\"Argument must be a I128\", nameof(value));\n        }\n    }\n\n    /// <inheritdoc cref=\"IComparable{T}.CompareTo(T)\" />\n    public int CompareTo(I128 value)\n    {\n        if (this < value)\n        {\n            return -1;\n        }\n        else if (this > value)\n        {\n            return 1;\n        }\n        else\n        {\n            return 0;\n        }","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/bindings-csharp/BSATN.Runtime/BSATN/I128.cs#L31-L67","documentation":"I128 is the SpacetimeDB BSATN.Runtime 128-bit signed integer type. It implements IComparable, and the untyped CompareTo(object?) overload only accepts another I128 (or null, which sorts after). Passing any other type throws ArgumentException because an I128 cannot be meaningfully ordered against an unrelated value.","triggerScenarios":"Invoking ((IComparable)i128).CompareTo(obj) where the boxed argument is a long, decimal, I256, string, or any non-I128 type. Typical entry points: Array.Sort on an object[], ArrayList ordering, or comparing values deserialized as object.","commonSituations":"Sorting or searching heterogeneous collections; porting code that compared BigInteger or decimal against 128-bit ids; dynamic/object round-trips through JSON or old collection APIs.","solutions":["Use the typed CompareTo(I128) overload or the <, >, == operators instead of the object-based overload","Pattern-match and convert before comparing: if (other is I128 i) ... else convert explicitly","When sorting mixed data, homogenize the array to I128[] first or supply a custom IComparer<object>"],"exampleFix":"// before\nI128 a = GetAmount();\nobject other = 42L;\nint cmp = a.CompareTo(other); // ArgumentException: Argument must be a I128\n\n// after\nif (other is I128 other128)\n{\n    int cmp = a.CompareTo(other128);\n}","handlingStrategy":"type-guard","validationCode":"// Only invoke the untyped CompareTo when the boxed value is compatible\nstatic bool CanCompareWithI128(object? other) => other is null || other is SpacetimeDB.I128;","typeGuard":"static bool IsI128(object? value) => value is SpacetimeDB.I128;","tryCatchPattern":"try\n{\n    var cmp = ((IComparable)i128Value).CompareTo(boxed);\n}\ncatch (ArgumentException)\n{\n    // heterogeneous comparison: convert the operand to I128 or sort by type name instead\n}","preventionTips":["Prefer the typed CompareTo(I128) overload or the <, >, == operators","Keep collections homogeneous (I128[] rather than object[]) before sorting","Pattern-match boxed values (if (o is I128 x)) before comparing","Avoid object/dynamic round-trips for 128-bit ids"],"tags":["csharp","spacetimedb","bsatn","int128","icomparable"],"backgroundTag":"icomparable-type-mismatch","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}