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 or equal to) operator is a binary comparison operator that evaluates whether the value of its left-hand operand is strictly less than or equivalent to the value of its right-hand operand. It returns a Bool indicating the result of the evaluation.
lhs <= rhs

Protocol Requirements

For the <= operator to be available, 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.

Evaluation Mechanics

The expression yields true if lhs precedes rhs in the type’s defined sorting order, or if lhs and rhs are identical in value. Otherwise, it yields false.
let a: Int = 5
let b: Int = 10
let c: Int = 5

let result1: Bool = (a <= b) // true
let result2: Bool = (b <= a) // false
let result3: Bool = (a <= c) // true

Standard Library Implementation

In Swift, developers do not typically implement the <= operator directly when creating custom types. The Comparable protocol inherits from Equatable and only strictly requires the implementation of the < (less than) and == (equal to) operators. The Swift standard library provides a default implementation for <= as an extension on Comparable. Under the hood, Swift evaluates lhs <= rhs by negating the greater-than logic, effectively computing !(rhs < lhs). This minimizes boilerplate code while ensuring mathematical consistency across all comparison operators (<, <=, >, >=).
struct Metric: Comparable {
    let magnitude: Double
    
    // Only < and == are required.
    // <= is automatically synthesized by the standard library.
    static func < (lhs: Metric, rhs: Metric) -> Bool {
        return lhs.magnitude < rhs.magnitude
    }
    
    static func == (lhs: Metric, rhs: Metric) -> Bool {
        return lhs.magnitude == rhs.magnitude
    }
}

let m1 = Metric(magnitude: 1.0)
let m2 = Metric(magnitude: 2.0)
let evaluation = m1 <= m2 // true, resolved via default Comparable extension

Operator Precedence

The <= operator belongs to the ComparisonPrecedence group. It has a lower precedence than addition/subtraction operators (AdditionPrecedence) but a higher precedence than logical conjunction operators (LogicalConjunctionPrecedence). It is non-associative, meaning multiple <= operators cannot be chained together in a single expression without explicit parentheses.
Master Swift with Deep Grasping Methodology!Learn More