clockworklabs/SpacetimeDB · error · ArgumentException

Argument must be a U256

Error message

Argument must be a U256

What it means

U256 is the SpacetimeDB BSATN.Runtime 256-bit unsigned integer. Its untyped CompareTo(object?) overload accepts only a U256 or null (null sorts after); any other runtime type throws ArgumentException.

Solutions

  1. Use the typed CompareTo(U256) overload or comparison operators
  2. Convert the other operand to U256 explicitly before comparing
  3. Provide an IComparer that handles type dispatch for heterogeneous data

Example fix

// before
int cmp = u256.CompareTo(otherObject); // ArgumentException: Argument must be a U256

// after
int cmp = u256.CompareTo((U256)otherObject); // after verifying the runtime type
Defensive patterns

Strategy: type-guard

Validate before calling

static bool CanCompareWithU256(object? other) => other is null || other is SpacetimeDB.U256;

Type guard

static bool IsU256(object? value) => value is SpacetimeDB.U256;

Try / catch

try
{
    var cmp = ((IComparable)u256Value).CompareTo(boxed);
}
catch (ArgumentException)
{
    // convert operand to U256 or reject
}

Prevention

When it happens

Trigger: Comparing a U256 against a boxed value of another type (U128, long, string, Identity boxed differently) through the object-based IComparable path, e.g. Array.Sort on mixed object arrays.

Common situations: Ordering Identity-related values stored in object collections; generic comparison helpers that take object; ported code from BigInteger comparisons.

Related errors


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

Appendix: source

Thrown at crates/bindings-csharp/BSATN.Runtime/BSATN/U256.cs:47

    internal U128 Lower => _lower;

    internal U128 Upper => _upper;

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

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

View on GitHub (pinned to 6dee26c6ef)