var.opCmp

int opCmp(const var rhs) const @safe

Three-way comparison operator for var.

Enables use of `<`, `<=`, `>`, `>=` operators between var values.

Comparison rules:

  • Numeric types: Compared by numeric value with automatic type promotion. Mixed integer/floating pairs compare exactly: the integer operand is never rounded to double (which would lose distinctions above 2^53 and break order transitivity). Two floating operands compare as double. Integer pairs compare without wrap-around:
  • - Unsigned vs unsigned compares as ulong.
  • - Signed vs signed compares as long.
  • - Mixed signed/unsigned compares by numeric value (a negative signed value is
  • always less than any unsigned value).
  • - Strings: Compared lexicographically using D's built-in string comparison.
  • - Booleans: false < true.
  • - Characters: Compared by their Unicode code point value.
  • - NULL: NULL is considered less than any non-NULL value.
  • Two NULL values are equal (returns 0).
  • - Arrays: Compared element-by-element (lexicographic order).
  • Shorter arrays are less than longer arrays if all elements match.
  • - Objects: Not directly comparable; returns 0 (equal) if same reference
  • or both empty, otherwise comparison is undefined (returns 0).
  • - Mixed incompatible types: When types are incompatible for comparison
  • (e.g., string vs array), the result is based on type tag ordering. *

    Returns

    - Negative value if this < rhs
  • Zero if this == rhs
  • Positive value if this > rhs

Throws

VarException if array nesting exceeds the structural depth limit (legal shallow aliasing permits cycles; comparison cannot order them).