Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Optionals e o Gato de Schrödinger

Francesco
February 29, 2016

Optionals e o Gato de Schrödinger

Deckset and Playground of my "Optionals and the Schrödinger's Cat" Talk
Apresentação no CocoaHeads Campinas em Fevereiro de 2016

Francesco

February 29, 2016
Tweet

More Decks by Francesco

Other Decks in Programming

Transcript

  1. enum MyOptional { case Some case None } let nothing

    = MyOptional.None let something = MyOptional.Some
  2. Set

  3. map

  4. func map<U>(f: (T -> U)) -> MyOptional<U> { guard let

    value = self else { return .None } let mappedValue = f(value) return .Some(mappedValue) }
  5. func map<U>(f: (T throws -> U)) rethrows -> MyOptional<U> {

    guard let value = self else { return .None } let mappedValue = try f(value) return .Some(mappedValue) }
  6. @warn_unused_result func map<U>(f: (T throws -> U)) rethrows -> MyOptional<U>

    { guard let value = self else { return .None } let mappedValue = try f(value) return .Some(mappedValue) }
  7. @warn_unused_result func flatMap<U>(f: (T throws -> MyOptional<U>)) rethrows -> MyOptional<U>

    { guard let value = self else { return .None } let mappedValue = try f(value) return mappedValue }
  8. if let aUnwrapped = a { if let bUnwrapped =

    b { if let cUnwrapped = c { println("\(aUnwrapped) - \(bUnwrapped) - \(cUnwrapped)") } } }
  9. if let aUnwrapped = a, bUnwrapped = b, cUnwrapped =

    c { println("\(aUnwrapped) - \(bUnwrapped) - \(cUnwrapped)") }
  10. if let aUnwrapped = a, bUnwrapped = b, cUnwrapped =

    c { println("\(aUnwrapped) - \(bUnwrapped) - \(cUnwrapped)") }
  11. nil

  12. 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 public func map<U>(@noescape f: (Wrapped) throws -> U) rethrows -> U? /// Returns `nil` if `self` is nil, `f(self!)` otherwise. @warn_unused_result public func flatMap<U>(@noescape f: (Wrapped) throws -> U?) rethrows -> U? /// Create an instance initialized with `nil`. public init(nilLiteral: ()) } extension Optional : CustomDebugStringConvertible { /// A textual representation of `self`, suitable for debugging. public var debugDescription: String { get } }
  13. /// If `self == nil`, returns `nil`. Otherwise, returns `f(self!)`.

    @warn_unused_result public func map<U>(@noescape f: (Wrapped) throws -> U) rethrows -> U? { switch self { case .Some(let y): return .Some(try f(y)) case .None: return .None } }
  14. /// Returns `nil` if `self` is `nil`, `f(self!)` otherwise. @warn_unused_result

    public func flatMap<U>(@noescape f: (Wrapped) throws -> U?) rethrows -> U? { switch self { case .Some(let y): return try f(y) case .None: return .None } }
  15. @_transparent @warn_unused_result public func ?? <T> (optional: T?, @autoclosure defaultValue:

    () throws -> T) rethrows -> T { switch optional { case .Some(let value): return value case .None: return try defaultValue() } }
  16. License · My Code: MIT · Apple's Code: Apache ·

    Deckset: CC-BY-SA · Emoji art: supplied by Emoji One