Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The > (greater than) operator is a binary relational operator that evaluates whether the left-hand operand is strictly greater than the right-hand operand, returning a Bool value.
lhs > rhs

Type Requirements and Comparable

For nominal types, both the left-hand side (lhs) and right-hand side (rhs) operands must be of the exact same type, and that type must conform to the Comparable protocol. Under the hood, the operator is defined as a static method within the protocol:
static func > (lhs: Self, rhs: Self) -> Bool
Custom types conforming to Comparable receive the > operator automatically. This is not achieved through compiler synthesis, but rather provided as a standard default implementation via a protocol extension on Comparable. The standard library evaluates lhs > rhs by internally executing rhs < lhs. Therefore, a type only needs to satisfy the core Comparable requirements (< and ==) to unlock the > operator.

Evaluation Mechanics by Type

  • Numeric Types: Performs a standard mathematical magnitude comparison (e.g., Int, Double, Float).
  • Strings: Performs a strict, case-sensitive comparison based on the canonical equivalence of Unicode scalar values. This means characters that are linguistically identical are evaluated as equal, regardless of their underlying Unicode scalar composition (e.g., a single precomposed scalar versus a decomposed base scalar plus a combining scalar).
  • Tuples: Tuples are structural types and do not conform to protocols in Swift, including Comparable. However, the standard library provides overloaded > functions specifically for tuples, provided all corresponding elements in both tuples are of the same Comparable type. As of Swift 5.9 (which introduced value and type parameter packs), the previous six-element limit was removed, allowing tuples of any arity to be compared. The evaluation proceeds left-to-right and terminates at the first pair of elements that are not equal.
  • Arrays: The > operator is not supported for Array types. In Swift, Array does not conform to Comparable. Attempting to evaluate [1, 2] > [0, 3] results in a compiler error. To compare arrays lexicographically, developers must instead use the lexicographicallyPrecedes(_:) method.

Operator Precedence

The > operator belongs to the ComparisonPrecedence group. It has a lower precedence than nil-coalescing, casting, and mathematical operators, but a higher precedence than logical conjunction (&&) and disjunction (||) operators. It is non-associative, meaning you cannot chain multiple > operators together in a single expression without parentheses (e.g., a > b > c is invalid syntax).
Master Swift with Deep Grasping Methodology!Learn More