Share
Embed
Either で想定内のエラーと想定外のエラーを別ものにしたい!
Rx Ja Night Vol.2 (https://connpass.com/event/57150/)
Ryutaro Miyashita
June 12, 2017
Transcript
40 slides
-
3Y+BWBךؒٓ٦عٝسؚٔٝ 0CTFSWBCMFPO&SSPS 'MPXBCMFPO&SSPS 4JOHMFPO&SSPS $PNQMFUBCMFPO&SSPS
.BZCFPO&SSPS ؒٓ٦ך「ֽ《⯓כא -
⢽ ؙٔحؙ̔ؿ؋؎ٕ《䖤̔،حفٗ٦س RxBinding.clicks(button) .map { /* Get file */ }
.flatMap { /* Upload */ } ~~~~~ .subscribe({ /* Success */ }, { /* IOException or Request failure */ }) -
RxBinding.clicks(button) .map { /* Get file */ } .flatMap {
/* Upload */ } ~~~~~ .subscribe({ /* Success */ }, { /* IOException or Request failure */ }) ֿֿדPO&SSPSָ鸐濼ׁה ⢽ ؙٔحؙ̔ؿ؋؎ٕ《䖤̔،حفٗ٦س -
RxBinding.clicks(button) .map { /* Get file */ } .flatMap {
/* Upload */ } ~~~~~ .subscribe({ /* Success */ }, { /* IOException or Request failure */ }) 4VCTDSJCFSPO&SSPSחⳢ椚ָ獳 ⢽ ؙٔحؙ̔ؿ؋؎ٕ《䖤̔،حفٗ٦س -
RxBinding.clicks(button) .map { /* Get file */ } .flatMap {
/* Upload */ } ~~~~~ .subscribe({ /* Success */ }, { /* IOException or Request failure */ }) ؙٔحؙ؎كٝزכֲ굲דֿזְ ⢽ ؙٔحؙ̔ؿ؋؎ٕ《䖤̔،حفٗ٦س -
ؒٓ٦عٝسؚٔٝؔلٖ٦ةכ葿ղ֮ PO&SSPS3FUVSO PO&SSPS3FTVNF/FYU SFUSZ SFUSZ8IFO
ˑؒٓ٦˒גזֽ 3Y+BWBךؒٓ٦عٝسؚٔٝ -
ؿ؋؎ָٕ鼅䫛ׁגְזְךַ ؿ؋؎ָٕ《䖤דֹזַךַ ،حفٗ٦س㼎韋㢩ךؿ؋؎ٕ䕎䒭ךַ 8FC؟٦غ٦ָتؐٝ׃גְךַ ؔؿٓ؎ٝזךַ
ˑؒٓ٦˒ה♧〡ח鎉ג˘˘ ،فٔה׃גˑ姻׃ְ˒䮙⹛כؒٓ٦ -
RxBinding.clicks(button) .map { /* Get file */ } .flatMap {
/* Upload */ } ~~~~~ .subscribe({ if (it.isSuccessful) { /* Success */ } else { /* Request failure */ } }, { /* IOException */ }) PO/FYUח3FTVMU㘗ְזך굲לׅ -
public abstract class Either<A, B> { public static <A, B>
Either<A, B> left(A a) { new Left<>(a); } public static <A, B> Either<A, B> right(B b) { new Right<>(b); } public abstract boolean isLeft(); public abstract boolean isRight(); public abstract <X> X fold(Fold<A, X> l, Fold<B, X> r); } &JUIFSKBWB -
public class Left<A, B> extends Either<A, B> { private final
A a; public Left(A a) { this.a = a; } @Override public boolean isLeft() { return true; } @Override public boolean isRight() { return false; } @Override public <X> X fold(Fold<A, X> l, Fold<B, X> r) { return l.fold(a); } } -FGUKBWB 3JHIUKBWB -
&JUIFS אך㘗ךֲծְ׆ַ㘗ך⦼䭯א ؟ـؙٓأה׃ג-FGU3JHIU㘗ָ֮ 䧭⸆כ姻׃ְ SJHIU חַֽג3JHIUד鵤ׅ
㣟侁כ-FGUד鵤ׅ 3Y+BWBדכ-FGU5ISPXBCMFחׅה葺ֲׁ -
fun a(): Observable<Either<Throwable, String>> = Observable.create { it.onNext(Either.left(RuntimeException())) it.onNext(Either.left(RuntimeException())) it.onNext(Either.left(RuntimeException()))
it.onNext(Either.right(“Hello”)) it.onNext(Either.right(“World”)) it.onNext(Either.right(“Foo Bar Buzz”)) } ⢽ 0CTFSWBCMFDSFBUF -
fun a(): Observable<Either<Throwable, String>> = Observable.create { it.onNext(Either.left(RuntimeException())) it.onNext(Either.left(RuntimeException())) it.onNext(Either.left(RuntimeException()))
it.onNext(Either.right(“Hello”)) it.onNext(Either.right(“World”)) it.onNext(Either.right(“Foo Bar Buzz”)) } ⢽ 0CTFSWBCMFDSFBUF 䧭⸆儗כ4USJOH 㣟侁儗כ5ISPXBCMF䭯א&JUIFS -
fun a(): Observable<Either<Throwable, String>> = Observable.create { it.onNext(Either.left(RuntimeException())) it.onNext(Either.left(RuntimeException())) it.onNext(Either.left(RuntimeException()))
it.onNext(Either.right(“Hello”)) it.onNext(Either.right(“World”)) it.onNext(Either.right(“Foo Bar Buzz”)) } ⢽ 0CTFSWBCMFDSFBUF ⡦㔐ד㣟侁鵤ֿׅהָדֹ -
fun a(): Observable<Either<Throwable, String>> = Observable.create { it.onNext(Either.left(RuntimeException())) it.onNext(Either.left(RuntimeException())) it.onNext(Either.left(RuntimeException()))
it.onNext(Either.right(“Hello”)) it.onNext(Either.right(“World”)) it.onNext(Either.right(“Foo Bar Buzz”)) } ⢽ 0CTFSWBCMFDSFBUF 䧭⸆⡦㔐ד鵤ֿׅהָדֹ -
⢽ 0CTFSWBCMFDSFBUF a().subscribe({ either -> either.fold({ /* left, “it” is
Throwable */ }, { /* right, “it” is String */ }) }, { /* Error */ }) -
⢽ 0CTFSWBCMFDSFBUF a().subscribe({ either -> either.fold({ /* left, “it” is
Throwable */ }, { /* right, “it” is String */ }) }, { /* Error */ }) 4VCTDSJCFSPO/FYUך⚥חֲ媮「ֽ〡⡲ &JUIFS" #ד䭷㹀׃ˑ㘗˒ד穠卓「ֽ《 -
sealed class Either<out A, out B> { abstract val isLeft:
Boolean abstract val isRight: Boolean } &JUIFSLU -
&JUIFSLU data class Left<out A, out B>(val value: A) :
Either<A, B>() { override val isLeft: Boolean = true override val isRight: Boolean = false } data class Right<out A, out B>(val value: B) : Either<A, B>() { override val isLeft: Boolean = false override val isRight: Boolean = true } -
&JUIFSLU inline fun <A, B, X> Either<A, B>.fold( left: (A)
-> X, right: (B) -> X ): X = when (this) { is Left -> left(value) is Right -> right(value) } -
&JUIFSLU inline fun <A, B, X> Either<A, B>.fold( left: (A)
-> X, right: (B) -> X ): X = when (this) { is Left -> left(value) is Right -> right(value) } ꟼ侧「ֽ《ג -
&JUIFSLU inline fun <A, B, X> Either<A, B>.fold( left: (A)
-> X, right: (B) -> X ): X = when (this) { is Left -> left(value) is Right -> right(value) } XIFO䒭ד㘗ח䘔ׄג㹋遤ׅꟼ侧ⴖ剏ִ -
fun b(): Observable<Either<Throwable, String>> = Observable.create { it.onNext(Left(RuntimeException())) it.onNext(Left(RuntimeException())) it.onNext(Left(RuntimeException()))
it.onNext(Right(“Hello”)) it.onNext(Right(“World”)) it.onNext(Right(“Foo Bar Buzz”)) } ⢽ 0CTFSWBCMFDSFBUF -
fun b(): Observable<Either<Throwable, String>> = Observable.create { it.onNext(Left(RuntimeException())) it.onNext(Left(RuntimeException())) it.onNext(Left(RuntimeException()))
it.onNext(Right(“Hello”)) it.onNext(Right(“World”)) it.onNext(Right(“Foo Bar Buzz”)) } ⢽ 0CTFSWBCMFDSFBUF 刿חأحؗٔ剅ֻֿהָדֹ -
⢽ 0CTFSWBCMFDSFBUF b().subscribe({ either -> either.fold({ /* left, “it” is
Throwable */ }, { /* right, “it” is String */ }) }, { /* Error */ }) +BWBד㹀纏׃הֹה「ֽ《倯כずׄ -
ׁח typealias RxEither<T> = Either<Throwable, T> typealias RxRight<T> = Right<Throwable,
T> typealias RxLeft<T> = Left<Throwable, T> fun <T> Observable<T>.either(): Observable<RxEither<T>> = map { RxRight(it) as RxEither<T> } .onErrorReturn { RxLeft(it) } -
ׁח typealias RxEither<T> = Either<Throwable, T> typealias RxRight<T> = Right<Throwable,
T> typealias RxLeft<T> = Left<Throwable, T> fun <T> Observable<T>.either(): Observable<RxEither<T>> = map { RxRight(it) as RxEither<T> } .onErrorReturn { RxLeft(it) } -FGU㘗ָ5ISPXBCMFך㘗UZQFBMJBTד欽䠐 -
ׁח typealias RxEither<T> = Either<Throwable, T> typealias RxRight<T> = Right<Throwable,
T> typealias RxLeft<T> = Left<Throwable, T> fun <T> Observable<T>.either(): Observable<RxEither<T>> = map { RxRight(it) as RxEither<T> } .onErrorReturn { RxLeft(it) } 䭁䓸ꟼ侧ד0CTFSWBCMFFJUIFS欽䠐 -
ׁח fun c(): Observable<String> = Observable.create { it.onNext(“Hello, world”) it.onError(RuntimeException())
} c().either().subscribe({ either -> either.fold({ /* left, “it” is Throwable */ }, { /* right, “it” is String */ }) }, { /* Error */ }) -
ׁח fun c(): Observable<String> = Observable.create { it.onNext(“Hello, world”) it.onError(RuntimeException())
} c().either().subscribe({ either -> either.fold({ /* left, “it” is Throwable */ }, { /* right, “it” is String */ }) }, { /* Error */ }) 兛鸐ך0CTFSWBCMFָ֮ג -
ׁח fun c(): Observable<String> = Observable.create { it.onNext(“Hello, world”) it.onError(RuntimeException())
} c().either().subscribe({ either -> either.fold({ /* left, “it” is Throwable */ }, { /* right, “it” is String */ }) }, { /* Error */ }) 䭁䓸ꟼ侧דFJUIFSؔلٖ٦ةך״ֲח䮶莸ֲ -
ׁח fun c(): Observable<String> = Observable.create { it.onNext(“Hello, world”) it.onError(RuntimeException())
} c().either().subscribe({ either -> either.fold({ /* left, “it” is Throwable */ }, { /* right, “it” is String */ }) }, { /* Error */ }) 0CTFSWBCMFNFSHFזוד䕵甧א -
תה 䟝㹀ⰻך㣟侁䟝㹀㢩ך㣟侁ⴓְֽ &JUIFS㘗⡲גⴓֽֿהָדֹ ؔلٖ٦ة٦דכזֻ㘗דⴓֽ ,PUMJO⢪ֲה刿חأو٦زח㹀纏דֹ
䭁䓸ꟼ侧⢪ג傀㶷ך0CTFSWBCMF &JUIFS5ISPXBCMF 5ח㢌䳔〳腉 &JUIFS荈⡤כ4DBMBחהؼٝزָ֮ -FGU 3JHIUךNBQװTXBQזוזו