Share
Embed
Transcript
34 slides
-
Copyright © Sansan, Inc. All rights reserved. > sealed class
in Kotlin1.1 Jumpei Yamamoto 2017.4.4 第5回 Kotlin勉強会 in Sansan #kotlin_sansan -
Copyright © Sansan, Inc. All rights reserved. > ⾃⼰紹介 -
⼭本純平 - Sansan株式会社 Eight事業部 - EightのAndroidアプリの開発 - twitter: @boohbah - github: https://github.com/yamamotoj -
Copyright © Sansan, Inc. All rights reserved. > Agenda -
テーマ:sealed class in Kotlin 1.1 - sealed classとは - Kotlin 1.1での変更点 -
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() } -
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ΦϒδΣΫτ ͻͱͭͷΛද͢ -
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() } ҾΛ࣋ͭΫϥε ͋Δू߹Λද͢ -
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અ͕͍Βͳ͍ -
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) } ϏϧυΤϥʔ -
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) } ཏੑΛνΣοΫ -
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) } } ϏϧυΤϥʔʹͳΒͳ͍ͷͰҙ -
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) } εϚʔτΩϟετͰ͕ΛͱΓͩͤΔ -
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型は直和型ではない) -
Copyright © Sansan, Inc. All rights reserved. > 代数的データ型の例 sealed
class Bool { object True : Bool() object False : Bool() } -
Copyright © Sansan, Inc. All rights reserved. > 代数的データ型の例 sealed
class Optional<T> { class Some<T>(val v:T) : Option<T>() object None: Option<Nothing>() } Nothing型 -> すべてのクラスのサブクラスとなる -
Copyright © Sansan, Inc. All rights reserved. > sealed class
(Kotlin 1.0) 使いづらかった点 -
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) } -
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Λ͚ͭΔඞཁ͕͋Δ -
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 -
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 } } } -
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 のサブクラスは親クラスと同じファイル上 に定義する必要がある。 -
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) } スッキリ! -
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() -
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を定義できる -
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 -
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) } -
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) } -
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 -
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) } } ちょっとパターンマッチっぽい (あと⼀歩!) -
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) -
Copyright © Sansan, Inc. All rights reserved. > Kotlin 1.1
sealed class まとめ - sealed classの⼦クラスをトップレベルに配置できるよう になって使⽤時にスッキリ! - sealed classの⼦クラスに data classを持てるようになっ た - インスタンス同⼠の⽐較がしやすくなった - destructing declarations でちょっとスッキリ -
$PQZSJHIU4BOTBO *OD"MMSJHIUTSFTFSWFE 4BOTBOҰॹʹ৽͍͠ՁΛ࡞͍ͬͯ͘ ؒΛ͕͍ͯ͞͠·͢ɻ 3VCZ 3VCZPO3BJMT ʢ8FCΞϓϦέʔγϣϯʣ $ɼ"41/&5.7$ ʢ8FCΞϓϦέʔγϣϯʣ
J04"OESPJEΞϓϦ ݸਓ໊͚ཧΞϓϦʮ&JHIUʯ ໊σʔλԽࢄॲཧγεςϜ ๏ਓ໊͚ཧαʔϏεʮ4BOTBOʯ ๏ਓ໊͚ཧαʔϏε ʮ4BOTBOʯ ݸਓ໊͚ཧΞϓϦʮ&JHIUʯ ΤϯδχΞืूத 4BOTBO࠾༻ ݕࡧ SFDSVJU!TBOTBODPN·Ͱ ͓ؾܰʹ͝࿈བྷ͍ͩ͘͞ɻ ڵຯͷ͋Δํ