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͍ͨ͠
    Keishin Yokomaku (@KeithYokoma) / Giftmall, Inc.
    potatotips #81

    View full-size slide

  2. KotlinͰEither͍ͨ͠
    About me
    ▸ @KeithYokoma


    ▸ Giftmall, Inc.


    ▸ Android App Engineer


    ▸ https://keithyokoma.dev/
    potatotips #81
    2

    View full-size slide

  3. 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

    View full-size slide

  4. 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

    View full-size slide

  5. 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

    View full-size slide

  6. DX Improvements
    ͦ͜Ͱ Either Ͱ͢Α
    ▸ Either


    ▸ L͔Rͷ͍ͣΕ͔ͷܕʹͳΓ͑Δ஋Λද͢ܕ


    ▸ ؔ਺ܕͷ Either Ϟφυͷ࣮૷Ͱ Scala ͳͲʹଘࡏ͢Δ


    ▸ TypeScript Ͱ͸֓೦ΛΑΓ֦ு͠ Union Type ͱͯ͠ఆٛͯ͋͠Δ
    6
    potatotips #81

    View full-size slide

  7. 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

    View full-size slide

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

    View full-size slide

  9. DX Improvements
    arrow-kt to the rescue?
    ▸ Either ͳͲଟ਺ͷؔ਺ܕͷ֓೦Λ࣮૷͍ͯ͠ΔϥΠϒϥϦ


    ▸ https://arrow-kt.io/


    ▸ Kotlin Ͱؔ਺ܕϓϩάϥϛϯάΛ͍ͨ͠ͳΒಋೖ͠Α͏
    9
    potatotips #81

    View full-size slide

  10. DX Improvements
    ׬
    10
    potatotips #81

    View full-size slide

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

    View full-size slide

  12. DX Improvements
    େ͖͗͢Δarrow-kt
    12
    ▸ ؔ਺ܕϓϩάϥϛϯά͸ٻΊ͍ͯͳ͍


    ▸ Foo ͔ Bar ͷͲͪΒ͔Λฦؔ͢਺Λܕ҆શʹ࡞Γ͍͚ͨͩ


    ▸ Either ͚ͩͭ·Έ৯͍͍͕ͨ͠ɺarrow-ktΛೖΕͨΒͦΕҎ֎΋ೖͬͯ͘Δ


    ▸ ࣗ෼Ͱ Either Λ࣮૷ͯ͠΋͍͍͕ɺ΋ͬͱ͔ΜͨΜʹ໨తΛୡ੒Ͱ͖ͳ͍͔
    potatotips #81

    View full-size slide

  13. DX Improvements
    sealed classes to the rescue
    13
    ▸ Kotlinʹ͓͚Δಛผͳந৅Ϋϥεɺ͋Δ͍͸ந৅ΠϯλϑΣʔε


    ▸ ಉ͡ϑΝΠϧʹ۩৅Ϋϥεɺ࣮૷ΫϥεΛॻ੍͘໿͕͋Δ
    potatotips #81

    View full-size slide

  14. 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

    View full-size slide

  15. 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

    View full-size slide

  16. 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

    View full-size slide

  17. DX Improvements
    Why sealed classes?
    17
    ▸ ಉ͡ϑΝΠϧʹ۩৅Ϋϥεɺ࣮૷ΫϥεΛॻ੍͘໿


    ▸ ίϯύΠϧ࣌ʹ͢΂ͯͷ۩৅Ϋϥεɺ࣮૷Ϋϥε͕೺ѲͰ͖Δ


    ▸ ܕʹΑΔ৚݅෼ذͷ໢ཏੑΛڧ੍Ͱ͖Δ


    ▸ ௨ৗͷ abstract class ΍ interface ʹ͸ͳ͍ػೳ
    potatotips #81

    View full-size slide

  18. 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

    View full-size slide

  19. 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
    💥

    View full-size slide

  20. 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
    👍

    View full-size slide

  21. DX Improvements
    EitherͷಛԽܕ
    21
    ▸ kotlin.Result


    ▸ Either ͷ͜ͱ


    ▸ ݁Ռ͕ਖ਼ৗ͔ҟৗ͔Λද͢


    ▸ Optional


    ▸ ஋͕ଘࡏ͢Δ͔ଘࡏ͠ͳ͍͔


    ▸ Scala ͩͱOptionܕʢ࣮ࡍͷ஋͸Some͔Noneʣ


    ▸ null ҆શ
    potatotips #81

    View full-size slide

  22. DX Improvements
    ͓ΘΓʹ
    22
    ▸ ΍Γ͍ͨ͜ͱ͸ Either ͕ͩඞͣ͠΋ͦͷ··ͷ࣮૷͕ඞཁͱ͸ݶΒͳ͍


    ▸ sealed classes Ͱ΋े෼දݱͰ͖Δ


    ▸ sealed classes ͳΒ3Ҏ্ͷܕͷ૊Έ߹Θ͕ͤͰ͖Δ


    ▸ A or B or C or …


    ▸ TypeScript ͷ Union Type ͷΑ͏ʹͰ͖Δ
    potatotips #81

    View full-size slide

  23. DX Improvements
    ͓ΘΓʹ
    23
    ▸ ͦΕͰ΋Eitherͷ࣮૷͕΄͘͠ͳΔͱ͖


    ▸ ֎෦ϥΠϒϥϦͷఆ͍ٛͯ͠ΔܕΛ࢖͏ͱ͖
    potatotips #81

    View full-size slide

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

    View full-size slide