Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Optional type: how .None == nil works

Tags:

swift

optional

I'm trying to understand how does it work:

  1> func returnNone() -> String? { return .None }
  2> returnNone() == nil
$R0: Bool = true
  3> returnNone() == .None
$R1: Bool = true

Why .None is equal nil.

I don't see anything about it in enum definition:

public enum Optional<Wrapped> : _Reflectable, NilLiteralConvertible {
    case None
    case Some(Wrapped)
    /// Construct a `nil` instance.
    public init()
    /// Construct a non-`nil` instance that stores `some`.
    public init(_ some: Wrapped)
    /// If `self == nil`, returns `nil`.  Otherwise, returns `f(self!)`.
    @warn_unused_result
    @rethrows public func map<U>(@noescape f: (Wrapped) throws -> U) rethrows -> U?
    /// Returns `nil` if `self` is nil, `f(self!)` otherwise.
    @warn_unused_result
    @rethrows public func flatMap<U>(@noescape f: (Wrapped) throws -> U?) rethrows -> U?
    /// Create an instance initialized with `nil`.
    public init(nilLiteral: ())
}
like image 445
Arsen Avatar asked Oct 19 '15 08:10

Arsen


1 Answers

enum Optional conforms to the NilLiteralConvertible protocol, which means that it can be initialized with the "nil" literal. The result is Optional<T>.None where the type placeholder T must be inferred from the context.

As an example,

let n = nil // type of expression is ambiguous without more context

does not compile, but

let n : Int? = nil

does, and the result is Optional<Int>.None.

Now optionals can in general not be compared if the underlying type is not Equatable:

struct ABC { }

let a1 : ABC? = ABC()
let a2 : ABC? = ABC()

if a1 == a2 { } // binary operator '==' cannot be applied to two 'ABC?' operands

and even this does not compile:

if a1 == Optional<ABC>.None { } // binary operator '==' cannot be applied to two 'ABC?' operands

But this compiles:

if a1 == nil { } 

It uses the operator

public func ==<T>(lhs: T?, rhs: _OptionalNilComparisonType) -> Bool

where _OptionalNilComparisonType is not documented officially. In https://github.com/andelf/Defines-Swift/blob/master/Swift.swift the definition can be found as (found by @rintaro and @Arsen, see comments):

struct _OptionalNilComparisonType : NilLiteralConvertible {
  init(nilLiteral: ())
}

This allows the comparison of any optional type with "nil", regardless of whether the underlying type is Equatable or not.

In short – in the context of Optionalnil can be thought of as a shortcut to .None, but the concrete type must be inferred from the context. There is a dedicated == operator for comparison with "nil".

like image 69
Martin R Avatar answered Oct 28 '22 21:10

Martin R