Slide 1

Slide 1 text

KotlinͰEither͍ͨ͠ Keishin Yokomaku (@KeithYokoma) / Giftmall, Inc. potatotips #81

Slide 2

Slide 2 text

KotlinͰEither͍ͨ͠ About me ▸ @KeithYokoma ▸ Giftmall, Inc. ▸ Android App Engineer ▸ https://keithyokoma.dev/ potatotips #81 2

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

DX Improvements ͦ͜Ͱ Either Ͱ͢Α ▸ Either ▸ L͔Rͷ͍ͣΕ͔ͷܕʹͳΓ͑Δ஋Λද͢ܕ ▸ ؔ਺ܕͷ Either Ϟφυͷ࣮૷Ͱ Scala ͳͲʹଘࡏ͢Δ ▸ TypeScript Ͱ͸֓೦ΛΑΓ֦ு͠ Union Type ͱͯ͠ఆٛͯ͋͠Δ 6 potatotips #81

Slide 7

Slide 7 text

KotlinͰEither͍ͨ͠ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢਺Λ࡞Γ͍ͨ 7 data class Foo(val name: String) data class Bar(val name: String) fun getSomething(): Either = if (flag) { Either.Left(Foo("foo")) } else { Either.Right(Bar("bar")) } potatotips #81

Slide 8

Slide 8 text

DX Improvements ͦΜͳ΋ͷ͸ Kotlin ͷݴޠ࢓༷΍ඪ४ϥΠϒϥϦʹ͸ͳ͍ 8 😥 potatotips #81

Slide 9

Slide 9 text

DX Improvements arrow-kt to the rescue? ▸ Either ͳͲଟ਺ͷؔ਺ܕͷ֓೦Λ࣮૷͍ͯ͠ΔϥΠϒϥϦ ▸ https://arrow-kt.io/ ▸ Kotlin Ͱؔ਺ܕϓϩάϥϛϯάΛ͍ͨ͠ͳΒಋೖ͠Α͏ 9 potatotips #81

Slide 10

Slide 10 text

DX Improvements ׬ 10 potatotips #81

Slide 11

Slide 11 text

DX Improvements ׬ 11 ▸ Ͱ͸ͳ͍ potatotips #81

Slide 12

Slide 12 text

DX Improvements େ͖͗͢Δarrow-kt 12 ▸ ؔ਺ܕϓϩάϥϛϯά͸ٻΊ͍ͯͳ͍ ▸ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢਺Λܕ҆શʹ࡞Γ͍͚ͨͩ ▸ Either ͚ͩͭ·Έ৯͍͍͕ͨ͠ɺarrow-ktΛೖΕͨΒͦΕҎ֎΋ೖͬͯ͘Δ ▸ ࣗ෼Ͱ Either Λ࣮૷ͯ͠΋͍͍͕ɺ΋ͬͱ͔ΜͨΜʹ໨తΛୡ੒Ͱ͖ͳ͍͔ potatotips #81

Slide 13

Slide 13 text

DX Improvements sealed classes to the rescue 13 ▸ Kotlinʹ͓͚Δಛผͳந৅Ϋϥεɺ͋Δ͍͸ந৅ΠϯλϑΣʔε ▸ ಉ͡ϑΝΠϧʹ۩৅Ϋϥεɺ࣮૷ΫϥεΛॻ੍͘໿͕͋Δ potatotips #81

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

DX Improvements Why sealed classes? 17 ▸ ಉ͡ϑΝΠϧʹ۩৅Ϋϥεɺ࣮૷ΫϥεΛॻ੍͘໿ ▸ ίϯύΠϧ࣌ʹ͢΂ͯͷ۩৅Ϋϥεɺ࣮૷Ϋϥε͕೺ѲͰ͖Δ ▸ ܕʹΑΔ৚݅෼ذͷ໢ཏੑΛڧ੍Ͱ͖Δ ▸ ௨ৗͷ abstract class ΍ interface ʹ͸ͳ͍ػೳ potatotips #81

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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 💥

Slide 20

Slide 20 text

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 👍

Slide 21

Slide 21 text

DX Improvements EitherͷಛԽܕ 21 ▸ kotlin.Result ▸ Either ͷ͜ͱ ▸ ݁Ռ͕ਖ਼ৗ͔ҟৗ͔Λද͢ ▸ Optional ▸ ஋͕ଘࡏ͢Δ͔ଘࡏ͠ͳ͍͔ ▸ Scala ͩͱOptionܕʢ࣮ࡍͷ஋͸Some͔Noneʣ ▸ null ҆શ potatotips #81

Slide 22

Slide 22 text

DX Improvements ͓ΘΓʹ 22 ▸ ΍Γ͍ͨ͜ͱ͸ Either ͕ͩඞͣ͠΋ͦͷ··ͷ࣮૷͕ඞཁͱ͸ݶΒͳ͍ ▸ sealed classes Ͱ΋े෼දݱͰ͖Δ ▸ sealed classes ͳΒ3Ҏ্ͷܕͷ૊Έ߹Θ͕ͤͰ͖Δ ▸ A or B or C or … ▸ TypeScript ͷ Union Type ͷΑ͏ʹͰ͖Δ potatotips #81

Slide 23

Slide 23 text

DX Improvements ͓ΘΓʹ 23 ▸ ͦΕͰ΋Eitherͷ࣮૷͕΄͘͠ͳΔͱ͖ ▸ ֎෦ϥΠϒϥϦͷఆ͍ٛͯ͠ΔܕΛ࢖͏ͱ͖ potatotips #81

Slide 24

Slide 24 text

KotlinͰEither͍ͨ͠ Keishin Yokomaku (@KeithYokoma) / Giftmall, Inc. potatotips #81