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.

If either operand is floating-point, both are compared as double. Otherwise, integers are compared 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