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

SwiftのEnum Associated Valuesを Kotlinでエミュレートしたい

tommykw
April 06, 2017

SwiftのEnum Associated Valuesを Kotlinでエミュレートしたい

tommykw

April 06, 2017
Tweet

More Decks by tommykw

Other Decks in Technology

Transcript

  1. enum ListItemType { case header(Int, String) case row1(Int, String, Int)

    case row2(Int, String) case row3(Int, String) case footer(Int, String) } 4XJGU&OVN 4XJGU&OVN"TTPDJBUFE7BMVFTͱ͸
  2. enum class ListItemType(val id: Int, val title: String) {
 HEADER(1,

    "header"),
 ROW1(2, "row1"),
 ROW2(3, "row2"),
 ROW3(4, "row3"),
 FOOTER(5, "footer"),
 } ,PUMJO&OVN 4XJGU&OVN"TTPDJBUFE7BMVFTͱ͸
  3. enum class ListItemType(val id: Int, val title: String) {
 HEADER(1,

    "header"),
 ROW1(2, "row1", “image_url"), // ίϯύΠϧΤϥʔ
 ROW2(3, "row2"),
 ROW3(4, "row3"),
 FOOTER(5, "footer"),
 } ,PUMJO&OVN 4XJGU&OVN"TTPDJBUFE7BMVFTͱ͸
  4. ΞϓϩʔνʢجఈΫϥεʣ open class BaseListItem(
 open val id: Int,
 open val

    title: String
 )
 
 data class ListItem1(
 override val id: Int,
 override val title: String,
 val mainImageUrl: Int
 ) : BaseListItem(id, title)
 
 data class ListItem2(
 override val id: Int,
 override val title: String,
 val mainImageUrl: String,
 val subImageUrl: String
 ) : BaseListItem(id, title) ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸
  5. ΞϓϩʔνʢλΠϓ෼͚ʣ enum class ListItemType {
 HEADER, ROW1, ROW2, ROW3, FOOTER


    }
 
 data class ListItem(val id: String = "",
 val title: String = "",
 val type: ListItemType = ListItemType.HEADER)
 ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸
  6. sealed class ListItemType {
 class Header(val id: Int, val title:

    String) : ListItemType()
 class Footer(val id: Int, val title: String) : ListItemType()
 } ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸
  7. sealed class ListItemType {
 data class Header(val id: Int, val

    title: String) : ListItemType()
 data class Row1(val id: Int, val title: String, val imageUrl: String) : ListItemType()
 data class Row2(val id: Int, val title: String) : ListItemType()
 data class Row3(val id: Int, val title: String) : ListItemType()
 data class Footer(val id: Int, val title: String) : ListItemType()
 } ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸
  8. sealed class ListItemType {
 data class Header(val id: Int, val

    title: String) : ListItemType()
 data class Row1(val id: Int, val title: String, val imageUrl: String) : ListItemType()
 data class Row2(val id: Int, val title: String) : ListItemType()
 data class Row3(val id: Int, val title: String) : ListItemType()
 data class Footer() : ListItemType() // ίϯύΠϧΤϥʔ
 } ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸
  9. sealed class ListItemType {
 data class Header(val id: Int, val

    title: String) : ListItemType()
 data class Row1(val id: Int, val title: String, val imageUrl: String) : ListItemType()
 data class Row2(val id: Int, val title: String) : ListItemType()
 data class Row3(val id: Int, val title: String) : ListItemType()
 class Footer() : ListItemType()
 } ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸