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

Result<T> V.S. Result<T, E>

Result<T> V.S. Result<T, E>

Yasuhiro Inami

November 21, 2017
Tweet

More Decks by Yasuhiro Inami

Other Decks in Programming

Transcript

  1. // Proposal, Τϥʔܕύϥϝʔλͳ͠ public enum Result<Value> { case success(Value) case

    failure(Swift.Error) } // V.S. // Τϥʔܕύϥϝʔλ͋Γ public enum Result<Value, Error: Swift.Error> { case success(Value) case failure(Error) }
  2. Result<T, E> (V.S. Result<T>) • GOOD: ResultʹΤϥʔܕΛຒΊࠐΉ͜ͱ͕Ͱ͖Δ • ͲͷΤϥʔ͕ɺͲ͔͜Β΍ͬͯ͘Δ͔Λ஌Δ͜ͱ͕Ͱ͖Δ ʢΤϥʔυϝΠϯ͕ܕ͔Β൑அͰ͖Δʣ

    • ύλʔϯϚονϯά͕໢ཏత • BAD: υϝΠϯຖʹΤϥʔܕͷม׵͕ඞཁ • େྔͷ mapError ม׵ͱɺωετͨ͠enumΤϥʔܕఆٛ
  3. NoError ͱ͸ʁ • enum NoError: Swift.Error {} • ஋Λੜ੒͢Δ͜ͱ͕Ͱ͖ͳ͍ (ྫɿNever)

    • NoError ΋·ͨ Error • ୅਺తσʔλܕʹ͓͚Δʮ0ʯ • θϩܕʢϘτϜʣ͕͋Δͱɺܕͷදݱ͕ΑΓ๛͔ʹͳΔ
  4. // ࢖༻ྫɿReactiveSwift ͷ UI Binding // (`Error == NoError` ͷΈͰ࢖͑Δ)

    static func <~ <Source: BindingSource> ( provider: Self, source: Source ) -> Disposable? where Source.Value == Value, Source.Error == NoError { ... } // Bindingྫ (Note: Signal = Hot Observable) let alphaSignal: Signal<CGFloat, NoError> = ... view.reactive.alpha <~ alphaSignal
  5. NoError ͷ࢖༻ྫ • Signal<Value, NoError> • ΤϥʔΛྲྀ͞ͳ͍ Observable • RxSwift.Driver

    ౳ͷผͷܕΛ༻ҙ͢Δඞཁ͕ͳ͍ • Result<Value, NoError> • ੒ޭͷΈΛѻ͏ʢ Result<Value>Ͱ͸දݱͰ͖ͳ͍ʣ • ී௨ʹ Value Λ࢖͑͹ྑ͍Μ͡Όʁ → ྑ͘ͳ͍
  6. Result<Value, NoError> Value ͷཧ༝ ΋͠ɺObservable<T, E> -> Result<T, E> ʹม׵͢Δؔ਺

    ͕͋Δ৔߹ʢྫɿ ReactiveSwift.firstʣ // Note: SignalProducer = Cold Observable extension SignalProducer { public func first() -> Result<Value, Error>? { ... } }
  7. Result<Value, NoError> Value ͷཧ༝ ΋͠ɺΤϥʔܕύϥϝʔλͷͳ͍ Result<T>ͷੈքͩͱɾɾɾ extension SignalProducer { public

    func first() -> Result<Value>? { ... } } extension SignalProducer where Error == NoError { // ઈରʹΤϥʔ͠ͳ͍ͷͰɺ͜͜Ͱ͸`Result<Value>?` Λ࢖͍ͨ͘ͳ͍ public func first() -> Value? { ... } } ແବʹΦʔόʔϩʔυΛॻ͘ඞཁ͕ൃੜ !"
  8. ਺ֶʢݍ࿦ʣతʹݴ͏ͱɾɾɾ • Result<Value, NoError> Value • ྆ऀ͸ʮಉ͡ܕʯͰ͸ͳ͍͕ɺʮಉܕʢisomorphicʣʯ • ૬ޓม׵Ͱ͖Δʢٯࣹ͕ଘࡏʣ •

    ܕʹͩ͜ΘΔ → දݱྗ্͕͕Δ → ந৅ԽͰ͖Δ • Result<T> ΍ Observable<T> ͸දݱྗʹ๡͍͠ • NoError (θϩͷଘࡏ) ͸Ғେ