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

Either in Kotlin

Either in Kotlin

Keishin Yokomaku

February 21, 2023
Tweet

More Decks by Keishin Yokomaku

Other Decks in Programming

Transcript

  1. KotlinͰEither͍ͨ͠ About me ▸ @KeithYokoma ▸ Giftmall, Inc. ▸ Android

    App Engineer ▸ https://keithyokoma.dev/ potatotips #81 2
  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
  3. 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
  4. 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
  5. DX Improvements ͦ͜Ͱ Either Ͱ͢Α ▸ Either<L, R> ▸ L͔Rͷ͍ͣΕ͔ͷܕʹͳΓ͑Δ஋Λද͢ܕ

    ▸ ؔ਺ܕͷ Either Ϟφυͷ࣮૷Ͱ Scala ͳͲʹଘࡏ͢Δ ▸ TypeScript Ͱ͸֓೦ΛΑΓ֦ு͠ Union Type ͱͯ͠ఆٛͯ͋͠Δ 6 potatotips #81
  6. 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
  7. DX Improvements arrow-kt to the rescue? ▸ Either ͳͲଟ਺ͷؔ਺ܕͷ֓೦Λ࣮૷͍ͯ͠ΔϥΠϒϥϦ ▸

    https://arrow-kt.io/ ▸ Kotlin Ͱؔ਺ܕϓϩάϥϛϯάΛ͍ͨ͠ͳΒಋೖ͠Α͏ 9 potatotips #81
  8. DX Improvements େ͖͗͢Δarrow-kt 12 ▸ ؔ਺ܕϓϩάϥϛϯά͸ٻΊ͍ͯͳ͍ ▸ Foo ͔ Bar

    ͷͲͪΒ͔Λฦؔ͢਺Λܕ҆શʹ࡞Γ͍͚ͨͩ ▸ Either ͚ͩͭ·Έ৯͍͍͕ͨ͠ɺarrow-ktΛೖΕͨΒͦΕҎ֎΋ೖͬͯ͘Δ ▸ ࣗ෼Ͱ Either Λ࣮૷ͯ͠΋͍͍͕ɺ΋ͬͱ͔ΜͨΜʹ໨తΛୡ੒Ͱ͖ͳ͍͔ potatotips #81
  9. DX Improvements sealed classes to the rescue 13 ▸ Kotlinʹ͓͚Δಛผͳந৅Ϋϥεɺ͋Δ͍͸ந৅ΠϯλϑΣʔε

    ▸ ಉ͡ϑΝΠϧʹ۩৅Ϋϥεɺ࣮૷ΫϥεΛॻ੍͘໿͕͋Δ potatotips #81
  10. 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
  11. 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
  12. 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
  13. DX Improvements Why sealed classes? 17 ▸ ಉ͡ϑΝΠϧʹ۩৅Ϋϥεɺ࣮૷ΫϥεΛॻ੍͘໿ ▸ ίϯύΠϧ࣌ʹ͢΂ͯͷ۩৅Ϋϥεɺ࣮૷Ϋϥε͕೺ѲͰ͖Δ

    ▸ ܕʹΑΔ৚݅෼ذͷ໢ཏੑΛڧ੍Ͱ͖Δ ▸ ௨ৗͷ abstract class ΍ interface ʹ͸ͳ͍ػೳ potatotips #81
  14. 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
  15. 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 💥
  16. 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 👍
  17. DX Improvements EitherͷಛԽܕ 21 ▸ kotlin.Result<T> ▸ Either<Throwable, T> ͷ͜ͱ

    ▸ ݁Ռ͕ਖ਼ৗ͔ҟৗ͔Λද͢ ▸ Optional ▸ ஋͕ଘࡏ͢Δ͔ଘࡏ͠ͳ͍͔ ▸ Scala ͩͱOptionܕʢ࣮ࡍͷ஋͸Some͔Noneʣ ▸ null ҆શ potatotips #81
  18. DX Improvements ͓ΘΓʹ 22 ▸ ΍Γ͍ͨ͜ͱ͸ Either ͕ͩඞͣ͠΋ͦͷ··ͷ࣮૷͕ඞཁͱ͸ݶΒͳ͍ ▸ sealed

    classes Ͱ΋े෼දݱͰ͖Δ ▸ sealed classes ͳΒ3Ҏ্ͷܕͷ૊Έ߹Θ͕ͤͰ͖Δ ▸ A or B or C or … ▸ TypeScript ͷ Union Type ͷΑ͏ʹͰ͖Δ potatotips #81