Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xuyazhong/5cf2fadf80096a1ab30ec8c2e4eb90b8 to your computer and use it in GitHub Desktop.
Save xuyazhong/5cf2fadf80096a1ab30ec8c2e4eb90b8 to your computer and use it in GitHub Desktop.
Optional
https://github.com/apple/swift/blob/master/stdlib/public/core/Optional.swift
public enum Optional<Wrapped> : ExpressibleByNilLiteral {
// The compiler has special knowledge of Optional<Wrapped>, including the fact
// that it is an `enum` with cases named `none` and `some`.
/// The absence of a value.
///
/// In code, the absence of a value is typically written using the `nil`
/// literal rather than the explicit `.none` enumeration case.
case none
/// The presence of a value, stored as `Wrapped`.
case some(Wrapped)
/// Creates an instance that stores the given value.
@_transparent
public init(_ some: Wrapped) { self = .some(some) }
/// Creates an instance initialized with `nil`.
///
/// Do not call this initializer directly. It is used by the compiler when you
/// initialize an `Optional` instance with a `nil` literal. For example:
///
/// var i: Index? = nil
///
/// In this example, the assignment to the `i` variable calls this
/// initializer behind the scenes.
@_transparent
public init(nilLiteral: ()) {
self = .none
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment