我正在尝试理解它是如何工作的:
1> func returnNone() -> String? { return .None }
2> returnNone() == nil
$R0: Bool = true
3> returnNone() == .None
$R1: Bool = true
为什么.None
等于nil
。
在枚举定义中我没有看到任何相关内容:
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: ())
}
public func ==<T>(lhs: _OptionalNilComparisonType, rhs: T?) -> Bool
。_OptionalNilComparisonType
没有记录,但这似乎允许比较任何可选类型与“nil”,无论基础类型是否可比。 - Martin Rstruct _OptionalNilComparisonType : NilLiteralConvertible { init(nilLiteral: ()) }
- Arsen