clockworklabs/SpacetimeDB · error · ArgumentException

Argument must be a U128

Error message

Argument must be a U128

What it means

U128 is the SpacetimeDB BSATN.Runtime 128-bit unsigned integer. The untyped IComparable.CompareTo(object?) overload accepts only a U128 or null; any other type throws ArgumentException since cross-type ordering is undefined.

Solutions

  1. Use the typed CompareTo(U128) overload or the <, >, == operators
  2. Pattern-match (if (o is U128 x)) before comparing boxed values
  3. Keep sorted collections typed as U128[] instead of object[]

Example fix

// before
int cmp = u128.CompareTo(boxedLong); // ArgumentException: Argument must be a U128

// after
if (boxedLong is U128 other)
{
    int cmp = u128.CompareTo(other);
}
Defensive patterns

Strategy: type-guard

Validate before calling

static bool CanCompareWithU128(object? other) => other is null || other is SpacetimeDB.U128;

Type guard

static bool IsU128(object? value) => value is SpacetimeDB.U128;

Try / catch

try
{
    var cmp = ((IComparable)u128Value).CompareTo(boxed);
}
catch (ArgumentException)
{
    // convert operand or reject the mixed comparison
}

Prevention

When it happens

Trigger: Calling CompareTo(object?) with a boxed long, I128, U256, Guid, or other type — via Array.Sort on object[], ArrayList, or manual object comparisons.

Common situations: Sorting heterogeneous id collections; comparing Uuids or ids pulled out as object; dynamic code paths that box numerics before comparing.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@6dee26c6ef (2026-08-20). Data as JSON: /api/errors/6fb2321b4a9d1e24. Report an issue: GitHub.

Appendix: source

Thrown at crates/bindings-csharp/BSATN.Runtime/BSATN/U128.cs:77

        BinaryPrimitives.WriteUInt64BigEndian(bytes.AsSpan(8, 8), _lower);

        return bytes;
    }

    /// <inheritdoc cref="IComparable.CompareTo(object)" />
    public int CompareTo(object? value)
    {
        if (value is U128 other)
        {
            return CompareTo(other);
        }
        else if (value is null)
        {
            return 1;
        }
        else
        {
            throw new ArgumentException("Argument must be a U128", nameof(value));
        }
    }

    /// <inheritdoc cref="IComparable{T}.CompareTo(T)" />
    public int CompareTo(U128 value)
    {
        if (this < value)
        {
            return -1;
        }
        else if (this > value)
        {
            return 1;
        }
        else
        {
            return 0;
        }

View on GitHub (pinned to 6dee26c6ef)