Share
Embed
Either in Kotlin
Keishin Yokomaku
February 21, 2023
Transcript
24 slides
-
KotlinͰEither͍ͨ͠ About me ▸ @KeithYokoma ▸ Giftmall, Inc. ▸ Android
App Engineer ▸ https://keithyokoma.dev/ potatotips #81 2 -
KotlinͰEither͍ͨ͠ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢Λ࡞Γ͍ͨ 3 data class Foo(val name:
String) data class Bar(val name: String) fun getSomething() = if (flag) { Foo("foo") } else { Bar("bar") } potatotips #81 -
KotlinͰEither͍ͨ͠ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢Λ࡞Γ͍ͨ 4 data class Foo(val name:
String) data class Bar(val name: String) fun getSomething(): Any = if (flag) { Foo("foo") } else { Bar("bar") } potatotips #81 -
KotlinͰEither͍ͨ͠ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢Λ࡞Γ͍ͨ 5 data class Foo(val name:
String) data class Bar(val name: String) fun getSomething(): Any = if (flag) { Foo("foo") } else { Bar("bar") } 😇 potatotips #81 -
DX Improvements ͦ͜Ͱ Either Ͱ͢Α ▸ Either<L, R> ▸ L͔Rͷ͍ͣΕ͔ͷܕʹͳΓ͑ΔΛද͢ܕ
▸ ؔܕͷ Either Ϟφυͷ࣮Ͱ Scala ͳͲʹଘࡏ͢Δ ▸ TypeScript Ͱ֓೦ΛΑΓ֦ு͠ Union Type ͱͯ͠ఆٛͯ͋͠Δ 6 potatotips #81 -
KotlinͰEither͍ͨ͠ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢Λ࡞Γ͍ͨ 7 data class Foo(val name:
String) data class Bar(val name: String) fun getSomething(): Either<Foo, Bar> = if (flag) { Either.Left(Foo("foo")) } else { Either.Right(Bar("bar")) } potatotips #81 -
DX Improvements arrow-kt to the rescue? ▸ Either ͳͲଟͷؔܕͷ֓೦Λ࣮͍ͯ͠ΔϥΠϒϥϦ ▸
https://arrow-kt.io/ ▸ Kotlin ͰؔܕϓϩάϥϛϯάΛ͍ͨ͠ͳΒಋೖ͠Α͏ 9 potatotips #81 -
DX Improvements େ͖͗͢Δarrow-kt 12 ▸ ؔܕϓϩάϥϛϯάٻΊ͍ͯͳ͍ ▸ Foo ͔ Bar
ͷͲͪΒ͔Λฦؔ͢Λܕ҆શʹ࡞Γ͍͚ͨͩ ▸ Either ͚ͩͭ·Έ৯͍͍͕ͨ͠ɺarrow-ktΛೖΕͨΒͦΕҎ֎ೖͬͯ͘Δ ▸ ࣗͰ Either Λ࣮͍͍͕ͯ͠ɺͬͱ͔ΜͨΜʹతΛୡͰ͖ͳ͍͔ potatotips #81 -
DX Improvements sealed classes to the rescue 13 ▸ Kotlinʹ͓͚ΔಛผͳநΫϥεɺ͋Δ͍நΠϯλϑΣʔε
▸ ಉ͡ϑΝΠϧʹ۩Ϋϥεɺ࣮ΫϥεΛॻ੍͕͋͘Δ potatotips #81 -
KotlinͰEither͍ͨ͠ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢Λ࡞Γ͍ͨ 14 data class Foo(val name:
String) data class Bar(val name: String) fun getSomething() = if (flag) { Foo("foo") } else { Bar("bar") } potatotips #81 -
KotlinͰEither͍ͨ͠ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢Λ࡞Γ͍ͨ 15 sealed class Something data
class Foo(val name: String) : Something() data class Bar(val name: String) : Something() fun getSomething() = if (flag) { Foo("foo") } else { Bar("bar") } potatotips #81 -
KotlinͰEither͍ͨ͠ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢Λ࡞Γ͍ͨ 16 sealed class Something data
class Foo(val name: String) : Something() data class Bar(val name: String) : Something() fun getSomething(): Something = if (flag) { Foo("foo") } else { Bar("bar") } potatotips #81 -
DX Improvements Why sealed classes? 17 ▸ ಉ͡ϑΝΠϧʹ۩Ϋϥεɺ࣮ΫϥεΛॻ੍͘ ▸ ίϯύΠϧ࣌ʹͯ͢ͷ۩Ϋϥεɺ࣮Ϋϥε͕ѲͰ͖Δ
▸ ܕʹΑΔ݅ذͷཏੑΛڧ੍Ͱ͖Δ ▸ ௨ৗͷ abstract class interface ʹͳ͍ػೳ potatotips #81 -
KotlinͰEither͍ͨ͠ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢Λ࡞Γ͍ͨ 18 sealed class Something data
class Foo(val name: String) : Something() data class Bar(val name: String) : Something() fun doSomething() { when (val something: Something = getSomething()) { is Foo -> {} is Bar -> {} } } potatotips #81 -
KotlinͰEither͍ͨ͠ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢Λ࡞Γ͍ͨ 19 sealed class Something data
class Foo(val name: String) : Something() data class Bar(val name: String) : Something() data class Baz(val name: String) : Something() fun doSomething() { when (val something: Something = getSomething()) { is Foo -> {} is Bar -> {} } } potatotips #81 💥 -
KotlinͰEither͍ͨ͠ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢Λ࡞Γ͍ͨ 20 sealed class Something data
class Foo(val name: String) : Something() data class Bar(val name: String) : Something() data class Baz(val name: String) : Something() fun doSomething() { when (val something: Something = getSomething()) { is Foo -> {} is Bar -> {} is Baz -> {} } } potatotips #81 👍 -
DX Improvements EitherͷಛԽܕ 21 ▸ kotlin.Result<T> ▸ Either<Throwable, T> ͷ͜ͱ
▸ ݁Ռ͕ਖ਼ৗ͔ҟৗ͔Λද͢ ▸ Optional ▸ ͕ଘࡏ͢Δ͔ଘࡏ͠ͳ͍͔ ▸ Scala ͩͱOptionܕʢ࣮ࡍͷSome͔Noneʣ ▸ null ҆શ potatotips #81 -
DX Improvements ͓ΘΓʹ 22 ▸ Γ͍ͨ͜ͱ Either ͕ͩඞͣͦ͠ͷ··ͷ࣮͕ඞཁͱݶΒͳ͍ ▸ sealed
classes ͰेදݱͰ͖Δ ▸ sealed classes ͳΒ3Ҏ্ͷܕͷΈ߹Θ͕ͤͰ͖Δ ▸ A or B or C or … ▸ TypeScript ͷ Union Type ͷΑ͏ʹͰ͖Δ potatotips #81