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

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

1821636ac95415aa6e84bc33b394283d?s=47 tommykw
April 06, 2017

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

1821636ac95415aa6e84bc33b394283d?s=128

tommykw

April 06, 2017
Tweet

Transcript

  1. Ո଒ͷຖ೔Λ΋ͬͱসإʹɻ 4XJGUͷ&OVN"TTPDJBUFE7BMVFTΛ ,PUMJOͰΤϛϡϨʔτ͍ͨ͠ ίωώτגࣜձࣾ

  2. ෋ా݈ೋ !UPNNZLX !UPNNZLX ίωώτגࣜձࣾ"OESPJE&OHJOFFS ϓϩϑΟʔϧ

  3. ΞδΣϯμ w 4XJGU&OVN"TTPDJBUFE7BMVFTͱ͸ w ,PUMJOͰΤϛϡϨʔτ͢Δํ๏ w ·ͱΊ

  4. Ո଒ͷຖ೔Λ΋ͬͱসإʹɻ 4XJGU&OVN"TTPDJBUFE7BMVFTͱ͸

  5. ͦ΋ͦ΋Կʁ 4XJGU&OVN"TTPDJBUFE7BMVFTͱ͸

  6. 4XJGUͷ&OVNܕʹͯ೚ҙͷܕΛ֨ೲ͢ Δ͜ͱ͕Ͱ͖·͢ 4XJGU&OVN"TTPDJBUFE7BMVFTͱ͸

  7. 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ͱ͸
  8. ศརʂʂ 4XJGU&OVN"TTPDJBUFE7BMVFTͱ͸

  9. ,PUMJO͸ʜ 4XJGU&OVN"TTPDJBUFE7BMVFTͱ͸

  10. 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ͱ͸
  11. 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ͱ͸
  12. ,PUMJOʹ͸&OVN"TTPDJBUFE7BMVFT ͷΑ͏ͳػೳ͸ఏڙ͞Ε͍ͯ·ͤΜ 4XJGU&OVN"TTPDJBUFE7BMVFTͱ͸

  13. ͪͳΈʹ4XJGU͚ͩͰͳ͘ɺଞݴޠͰ͸όϦΞϯ τܕͱ͍͏ܕͰಉ༷ͷػೳΛ࣮ݱͰ͖ΔΑ͏Ͱ ͢ 4XJGU&OVN"TTPDJBUFE7BMVFTͱ͸

  14. ϦετͷϏϡʔλΠϓ΍ΠϕϯτλΠϓΛ ಉ༷ͷػೳΛ࢖ͬͯ,PUMJOͰ؅ཧ͍ͨ͠ʂ 4XJGU&OVN"TTPDJBUFE7BMVFTͱ͸

  15. Ո଒ͷຖ೔Λ΋ͬͱসإʹɻ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  16. "OESPJE։ൃʹ͓͍ͯɺ3FDZDMFS7JFXͷ ෳ਺ϏϡʔλΠϓΛͲ͏΍ͬͯ؅ཧͯ͠ ͍·͔͢ʁ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  17. ϕʔεͱͳΔجఈΫϥεΛ࡞ΓɺͦΕ Λجʹͨ͠αϒΫϥεͰ؅ཧ͢Δ ΞϓϩʔνʢجఈΫϥεʣ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  18. ΞϓϩʔνʢجఈΫϥεʣ 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ͰΤϛϡϨʔτ͢Δʹ͸
  19. ΞϓϩʔνʢλΠϓ෼͚ʣ λΠϓΫϥεΛ࡞ͬͯ؅ཧ͢Δ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  20. ΞϓϩʔνʢλΠϓ෼͚ʣ enum class ListItemType {
 HEADER, ROW1, ROW2, ROW3, FOOTER


    }
 
 data class ListItem(val id: String = "",
 val title: String = "",
 val type: ListItemType = ListItemType.HEADER)
 ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸
  21. 4XJGUΈ͍ͨʹ&OVNͰ؅ཧͯ͠Έ͍ͨ ͳʜ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  22. ,PUMJOͰ΋ͬͱ؆୯ʹͰ͖ͳ͍͔ͳʜ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  23. 4FBMFE$MBTT ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  24. 4FBMFE$MBTT͸ܧঝΛ੍ݶͰ͖ɺωετ͠ ͨΫϥε΍ΦϒδΣΫτʹରͯ͠ར༻Ͱ͖Δ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  25. ྻڍܕΛ&OVNͰ͸ͳ͘ɺ4FBMFE $MBTTͰදݱ͢Δ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  26. sealed class ListItemType {
 class Header(val id: Int, val title:

    String) : ListItemType()
 class Footer(val id: Int, val title: String) : ListItemType()
 } ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸
  27. 3FMBYFE3VMFTGPS4FBMFE$MBTTFT TJODF ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  28. ,PUMJO͔Β4FBMFE$MBTTͰ%BUB $MBTTΛར༻Ͱ͖ΔΑ͏ʹͳΓ·ͨ͠ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  29. 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ͰΤϛϡϨʔτ͢Δʹ͸
  30. %BUB$MBTTʹ͸ɺFRVBMTɺIBTI$PEFɺ DPNQPOFOUɺDPQZͳͲ͕͋ͬͯศརʂ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  31. %BUB$MBTT͸σʔλͷ੔߹ੑΛकΔͨ ΊʹҾ਺͕ඞཁͱͳΓ·͢ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  32. 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ͰΤϛϡϨʔτ͢Δʹ͸
  33. Ҿ਺ͷͳ͍΋ͷʹ͍ͭͯ͸ɺ$MBTTͰ ఆٛ͢Ε͹ྑͦ͞͏ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  34. 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ͰΤϛϡϨʔτ͢Δʹ͸
  35. ϦετͷϏϡʔλΠϓ΍ΠϕϯτλΠ ϓͷ؅ཧָ͕ʹͳΓ·͢Ͷʂ ,PUMJOͰΤϛϡϨʔτ͢Δʹ͸

  36. Ո଒ͷຖ೔Λ΋ͬͱসإʹɻ ·ͱΊ

  37. 4XJGU&OVN"TTPDJBUFE7BMVFTͷ୅ସҊͱ͠ ͯɺ4FBMFE$MBTTͱ%BUB$MBTTΛ࢖ͬͯΈΔͷ ͸͍͔͕Ͱ͠ΐ͏͔ ·ͱΊ

  38. Ո଒ͷຖ೔Λ΋ͬͱসإʹɻ ίωώτגࣜձࣾ ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠