Two things happen here (whether we like it or not): First, there is an
operator
public func <(lhs: T?, rhs: T?) -> Bool
which compares two optionals if the underlying type is comparable.
The behavior is not documented (as far as I know), but it seems that
nil
aka Optional.None
is considered less than all non-nil
values Optional.Some(value)
.
Second, enum Optional
has a constructor
/// Construct a non-`nil` instance that stores `some`.
public init(_ some: Wrapped)
Now in
if x < 10 { ... }
the lhs has the type Optional
. The only candidate for the <
operator is the above-mentioned one comparing two optionals.
Therefore the rhs is inferred as an optional as well, so this
is equivalent to
if x < Optional.Some(10) { ... }
Update:
This feature has been removed in Swift 3 (SE-0121 – Remove Optional Comparison Operators) and that code no longer compiles with Xcode 8
(currently beta 6).