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

sealed class in Kotlin1.1

sealed class in Kotlin1.1

Jumpei Yamamoto

April 04, 2017
Tweet

More Decks by Jumpei Yamamoto

Other Decks in Programming

Transcript

  1. Copyright © Sansan, Inc. All rights reserved. > sealed class

    in Kotlin1.1 Jumpei Yamamoto 2017.4.4 第5回 Kotlin勉強会 in Sansan #kotlin_sansan
  2. Copyright © Sansan, Inc. All rights reserved. > ⾃⼰紹介 -

    ⼭本純平 - Sansan株式会社 Eight事業部 - EightのAndroidアプリの開発 - twitter: @boohbah - github: https://github.com/yamamotoj
  3. Copyright © Sansan, Inc. All rights reserved. > Agenda -

    テーマ:sealed class in Kotlin 1.1 - sealed classとは - Kotlin 1.1での変更点
  4. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() }
  5. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() } 4JOHMFUPOΦϒδΣΫτ ͻͱͭͷ஋Λද͢
  6. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() } Ҿ਺Λ࣋ͭΫϥε ͋Δू߹Λද͢
  7. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() } fun value(color: Color) = when(color){ Color.Red -> "#FF0000" Color.Green -> "#FF0000" Color.Blue -> "#FF0000" is Color.RGB ->"#%02X%02X%02X".format(color.r, color.g, color.b) } FMTFઅ͕͍Βͳ͍
  8. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() } fun value(color: Color) = when(color){ // Color.Red -> "#FF0000" Color.Green -> "#FF0000" Color.Blue -> "#FF0000" is Color.RGB ->"#%02X%02X%02X".format(color.r, color.g, color.b) } ϏϧυΤϥʔ
  9. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() } fun value(color: Color) = when(color){ Color.Red -> "#FF0000" Color.Green -> "#FF0000" Color.Blue -> "#FF0000" is Color.RGB ->"#%02X%02X%02X".format(color.r, color.g, color.b) } ໢ཏੑΛνΣοΫ
  10. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() } fun value(color: Color) { when(color){ // Color.Red -> "#FF0000" Color.Green -> "#FF0000" Color.Blue -> "#FF0000" is Color.RGB ->"#%02X%02X%02X".format(color.r, color.g, color.b) } } ϏϧυΤϥʔʹͳΒͳ͍ͷͰ஫ҙ
  11. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() } fun value(color: Color) = when(color){ Color.Red -> "#FF0000" Color.Green -> "#FF0000" Color.Blue -> "#FF0000" is Color.RGB ->"#%02X%02X%02X".format(color.r, color.g, color.b) } εϚʔτΩϟετͰ஋͕ΛͱΓͩͤΔ
  12. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() } 代数的データ型(直和型) - 互いに交わらない集合の和 - これ以外の値をもたない (厳密には上記Color型は直和型ではない)
  13. Copyright © Sansan, Inc. All rights reserved. > 代数的データ型の例 sealed

    class Bool { object True : Bool() object False : Bool() }
  14. Copyright © Sansan, Inc. All rights reserved. > 代数的データ型の例 sealed

    class Optional<T> { class Some<T>(val v:T) : Option<T>() object None: Option<Nothing>() } Nothing型 -> すべてのクラスのサブクラスとなる
  15. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() } fun value(color: Color) = when(color){ Color.Red -> "#FF0000" Color.Green -> "#FF0000" Color.Blue -> "#FF0000" is Color.RGB ->"#%02X%02X%02X".format(color.r, color.g, color.b) }
  16. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() } fun value(color: Color) = when(color){ Color.Red -> "#FF0000" Color.Green -> "#FF0000" Color.Blue -> "#FF0000" is Color.RGB ->"#%02X%02X%02X".format(color.r, color.g, color.b) } $PMPSΛ͚ͭΔඞཁ͕͋Δ
  17. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() } // ΦϒδΣΫτͲ͏͠Λൺֱ͢Δ val b = Color.RGB(0,0,0) == Color.RGB(0,0,0) b => false
  18. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.0) sealed class Color { object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color(){ override fun equals(other: Any?): Boolean { if (this === other) return true if (other?.javaClass != javaClass) return false other as RGB if (r != other.r) return false if (g != other.g) return false if (b != other.b) return false return true } override fun hashCode(): Int { var result = r result = 31 * result + g result = 31 * result + b return result } } }
  19. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.1) sealed class Color object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() - sealed class のサブクラスをトップレベルに記述できる - sealed class のサブクラスは親クラスと同じファイル上 に定義する必要がある。
  20. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.1) sealed class Color object Red : Color() object Green : Color() object Blue : Color() class RGB(val r: Int, val g: Int, val b: Int) : Color() fun value(color: Color) = when(color){ Red -> "#FF0000" Green -> "#FF0000" Blue -> "#FF0000" is RGB ->"#%02X%02X%02X".format(color.r, color.g, color.b) } スッキリ!
  21. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.1) sealed class Color object Red : Color() object Green : Color() object Blue : Color() data class RGB(val r: Int, val g: Int, val b: Int) : Color()
  22. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.1) sealed class Color object Red : Color() object Green : Color() object Blue : Color() data class RGB(val r: Int, val g: Int, val b: Int) : Color() data class - data classが親クラスをもつことができる - sealed class の⼦クラスとしてdata classを定義できる
  23. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.1) sealed class Color object Red : Color() object Green : Color() object Blue : Color() data class RGB(val r: Int, val g: Int, val b: Int) : Color() // ΦϒδΣΫτͲ͏͠Λൺֱ͢Δ val b = RGB(0,0,0) == RGB(0,0,0) b => true
  24. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.1) sealed class Color object Red : Color() object Green : Color() object Blue : Color() data class RGB(val r: Int, val g: Int, val b: Int) : Color() fun value(color: Color) = when(color){ Red -> "#FF0000" Green -> "#FF0000" Blue -> “#FF0000" RGB(0,0,0) -> “#000000” is RGB ->"#%02X%02X%02X".format(color.r, color.g, color.b) }
  25. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.1) sealed class Color object Red : Color() object Green : Color() object Blue : Color() data class RGB(val r: Int, val g: Int, val b: Int) : Color() fun value(color: Color) = when(color){ Red -> "#FF0000" Green -> "#FF0000" Blue -> “#FF0000" RGB(0,0,0) -> “#000000” is RGB ->"#%02X%02X%02X".format(color.r, color.g, color.b) }
  26. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.1) sealed class Color object Red : Color() object Green : Color() object Blue : Color() data class RGB(val r: Int, val g: Int, val b: Int) : Color() val (r, g, b) = RGB(0, 0, 0) data class の Destructuring Declarations
  27. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.1) sealed class Color object Red : Color() object Green : Color() object Blue : Color() data class RGB(val r: Int, val g: Int, val b: Int) : Color() fun value(color: Color) = when(color){ Red -> "#FF0000" Green -> "#FF0000" Blue -> “#FF0000" is RGB -> { val (r, g, b) = color “#%02X%02X%02X".format(r, g, b) } } ちょっとパターンマッチっぽい (あと⼀歩!)
  28. Copyright © Sansan, Inc. All rights reserved. > sealed class

    (Kotlin 1.1) sealed class Color object Red : Color() object Green : Color() object Blue : Color() data class RGB(val r: Int, val g: Int, val b: Int) : Color() fun value(color: Color) = when(color){ Red -> "#FF0000" Green -> "#FF0000" Blue -> “#FF0000" is RGB -> color.let { (r, g, b) -> “#%02X%02X%02X".format(r, g, b) } } Destructuring in Lambdas (since 1.1)
  29. Copyright © Sansan, Inc. All rights reserved. > Kotlin 1.1

    sealed class まとめ - sealed classの⼦クラスをトップレベルに配置できるよう になって使⽤時にスッキリ! - sealed classの⼦クラスに data classを持てるようになっ た - インスタンス同⼠の⽐較がしやすくなった - destructing declarations でちょっとスッキリ
  30. $PQZSJHIU˜4BOTBO *OD"MMSJHIUTSFTFSWFE  4BOTBO͸Ұॹʹ৽͍͠Ձ஋Λ࡞͍ͬͯ͘ ஥ؒΛ͕͍ͯ͞͠·͢ɻ 3VCZ 3VCZPO3BJMT ʢ8FCΞϓϦέʔγϣϯʣ $ɼ"41/&5.7$ ʢ8FCΞϓϦέʔγϣϯʣ

    J04"OESPJEΞϓϦ   ݸਓ޲໊͚ࢗ؅ཧΞϓϦʮ&JHIUʯ   ໊ࢗσʔλԽ෼ࢄॲཧγεςϜ   ๏ਓ޲໊͚ࢗ؅ཧαʔϏεʮ4BOTBOʯ   ๏ਓ޲໊͚ࢗ؅ཧαʔϏε ʮ4BOTBOʯ   ݸਓ޲໊͚ࢗ؅ཧΞϓϦʮ&JHIUʯ ΤϯδχΞืूத 4BOTBO࠾༻ ݕࡧ SFDSVJU!TBOTBODPN·Ͱ ͓ؾܰʹ͝࿈བྷ͍ͩ͘͞ɻ ڵຯͷ͋Δํ͸