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 < (less than) operator is a binary infix relational operator that evaluates whether the left-hand operand is strictly less in magnitude, value, or sorting order than the right-hand operand, returning a Bool. In Swift, the < operator serves as the foundational requirement for the Comparable protocol. Because Comparable inherits from Equatable, any type that implements both the < and == operators automatically receives default implementations for the >, <=, and >= operators via protocol extensions provided by the Swift standard library.

Syntax and Signature

The operator is defined as a static function within a type. The standard library signature requires both the left-hand side (lhs) and right-hand side (rhs) operands to be of the same type (Self):
static func < (lhs: Self, rhs: Self) -> Bool

Precedence and Associativity

The < operator belongs to the ComparisonPrecedence group in Swift’s parsing grammar.
  • Associativity: none. Because it lacks associativity, chained comparisons like a < b < c are syntactically invalid in Swift. The compiler requires explicit logical conjunctions (e.g., a < b && b < c).
  • Higher Precedence: Multiplication (*, /), addition (+, -), and nil-coalescing (??) operators belong to precedence groups that evaluate before comparison operators. This allows mathematical expressions to resolve prior to comparison (e.g., a + b < c evaluates as (a + b) < c).
  • Lower Precedence: Logical operators (&&, ||), the ternary operator (?:), and assignment operators (=) belong to precedence groups that evaluate after comparison operators. This allows chained logical checks to resolve correctly (e.g., a < b && c < d evaluates as (a < b) && (c < d)).

Custom Implementation

When conforming a custom struct, class, or enum to Comparable, the < operator must be explicitly implemented as a static method if compiler synthesis is not available.
struct Vector: Comparable {
    let magnitude: Double
    
    static func < (lhs: Vector, rhs: Vector) -> Bool {
        return lhs.magnitude < rhs.magnitude
    }
}

Compiler Synthesis

The Swift compiler can automatically synthesize the < operator implementation for specific types:
  1. Enumerations without associated values: The compiler synthesizes < based on the semantic declaration order of the case statements (top-to-bottom).
  2. Enumerations with associated values: The compiler synthesizes < if and only if all associated values themselves conform to Comparable. It evaluates the case declaration order first, and if the cases are identical, it evaluates the associated values lexicographically.
enum Status: Comparable {
    case idle
    case processing(Int)
    case completed
}
// Synthesis allows: Status.idle < Status.processing(1) -> true
Master Swift with Deep Grasping Methodology!Learn More