TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
< (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):
Precedence and Associativity
The< operator belongs to the ComparisonPrecedence group in Swift’s parsing grammar.
- Associativity:
none. Because it lacks associativity, chained comparisons likea < b < care 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 < cevaluates 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 < devaluates as(a < b) && (c < d)).
Custom Implementation
When conforming a customstruct, class, or enum to Comparable, the < operator must be explicitly implemented as a static method if compiler synthesis is not available.
Compiler Synthesis
The Swift compiler can automatically synthesize the< operator implementation for specific types:
- Enumerations without associated values: The compiler synthesizes
<based on the semantic declaration order of thecasestatements (top-to-bottom). - Enumerations with associated values: The compiler synthesizes
<if and only if all associated values themselves conform toComparable. It evaluates thecasedeclaration order first, and if the cases are identical, it evaluates the associated values lexicographically.
Master Swift with Deep Grasping Methodology!Learn More





