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

Mapped TypeとPlatform Typeの話

F46c97bb74758d481531990654933050?s=47 RyotaMurohoshi
December 13, 2016

Mapped TypeとPlatform Typeの話

2016/12/13(火)のSansanさんで開催された第4回 Kotlin Sansan勉強会での発表資料です。

F46c97bb74758d481531990654933050?s=128

RyotaMurohoshi

December 13, 2016
Tweet

Transcript

  1. Mapped&TypeͱPla+orm&Typeͷ࿩ @RyotaMurohoshi 2016/12/13(Ր))ୈ4ճKotlinษڧձ)ˏ)Sansan

  2. ࣗݾ঺հ • ࣨ੕྄ଠ(ΉΖ΄͠) • @RyotaMurohoshi • ࢓ࣄͰKotlin+Android

  3. None
  4. None
  5. ·ͣ؆୯ʹPla$orm(Typeͷ࿩Λͯ͠ ͦͷޙMapped&Typeͷ࿩Λ͠·͢ ʰ(Mutable)List<String!>!ʱܕͱ͔ͷ࿩΋

  6. ͬͦ͘͞࿩͸ԣಓʹͦΕ·͕͢ String?΍String!ɺͳΜͯಡΈ·͢ʁ

  7. ͞Βͬͱཧղ͢ΔPla$orm(Type

  8. ʰKotlinͩͬͨΒɺઈରNullPointerExep/onى͖ͳ͍ʱ ͜Ε͸ؒҧ͍ʂ

  9. Kotlinͱnull // ίϯύΠϧΤϥʔ val string : String = null //

    ͜Ε͸OK val string : String? = null
  10. String?Λฦؔ͢਺͕͋ͬͯ fun loadMessage(): String? = /*ུ*/

  11. // OK val message : String? = loadMessage() // ίϯύΠϧΤϥʔ

    val message : String = loadMessage() // Nullableܕ͸?.Ͱͳ͍ͱϝϯόΞΫηεͰ͖ͳ͍ // ΋͠message͕nullͳΒmessage?.length΋null println(message?.length) // ίϯύΠϧΤϥʔ println(message.length)
  12. Kotlin͚ͩͳΒ ͦ͏ͦ͏NullPointerExcep/onى͖ͳ͍ ࣮͸Kotlin͚ͩͰ΋NullPointerExcep0onग़ͤΔΒ͍͠

  13. Java͔ΒདྷͨΦϒδΣΫτ͸ʁ

  14. Java͔ΒདྷͨΦϒδΣΫτͷ࢖͍ํΛؒҧ͑Δͱ NullPointerExcep/onͱ͔IllegalStateExcep/onʹͳΔ

  15. Pla$orm(Type

  16. ݪଇJava͔ΒདྷͨΦϒδΣΫτ͸Pla&orm*Type

  17. Javaͷίʔυ public class Utility { public static String loadMessage() {

    /* ུ */ } }
  18. Kotlinͷίʔυ // IDEͩͱʮString!ʯͬͯදࣔ͞ΕΔ Platform Type val message = Utility.loadMessage() //

    String?ܕʹɻString!͸String?ʹ΋୅ೖՄ val message : String? = Utility.loadMessage() // StringܕʹɻString!͸Stringʹ΋୅ೖՄ val message : String = Utility.loadMessage()
  19. ஫ҙ఺

  20. IllegalStateExcep,onʹͳΔྫ

  21. IllegalStateExcep,onʹͳΔྫ // Platform TypeͷString!ܕΛStringܕʹ୅ೖͨ࣌͠ɺ // ͦͷΦϒδΣΫτ͕nullͳΒIllegalStateException val message: String =

    Utility.loadMessage()
  22. NullPointerExcep/onʹͳΔྫ

  23. NullPointerExcep/onʹͳΔྫ // String!ܕ val message = Utility.loadMessage() // Platform TypeͷString!ܕͷϝϯόʹΞΫηεͨ࣌͠ɺ

    // ͦͷΦϒδΣΫτ͕nullͩͬͨΒNullPointerException println(message.length) // ͜ΕͳΒOKɻnullʹͳΔ println(message?.length)
  24. ίϯύΠϧΤϥʔʹ͸ͳΒͳͯ͘΋ ࣮ߦ࣌ʹΤϥʔʹͳΔ͔Β஫ҙʂ

  25. @Nullable΍@NotNullΛ͏·͘࢖͓͏ʂ @NullableΛ͚ͭͨJavaͷString͸String? @NotNullΛ͚ͭͨJavaͷString͸String

  26. ۦ͚଍ͰPla$orm(Type ৄ͘͠͸ɺQiitaͷΞυϕϯτΧϨϯμʔݟͯͶ

  27. ͱ͜ΖͰPla$orm(Type͸Ϋϥεܕͷ࿩ ͡Ό͋intͱ͔doubleͱ͔booleanͱ͔ɺ primi%ve͸Ͳ͏ͳΔʁ

  28. Mapped&Type

  29. JavaͷintɾdoubleɾboolenͳͲ͸ KotlinͰ͸ɺkotlin.Intɺkotlin.Doubleɺkotlin.BooleanʹͳΓ·͢

  30. ݴ͍׵͑ΔͱJavaͷprimi(ve͸ɺ KotlinͰ͸ݻ༗ͷTypeͱͯ͠ѻΘΕΔ

  31. Mapped&Type

  32. (͜Ε͸υΩϡϝϯτʹॻ͍ͯ͋ͬͨ)

  33. ͡Ό͋JavaͷIntegerͱ͔Doubleͱ͔Boolenͱ͔͸ɺ primi%veܕͷϥούʔܕ͸Kotlin͔ΒͲ͏ݟ͑Δʁ (ͳ͔ͥυΩϡϝϯτʹͳ͔ͬͨ)

  34. JavaͷInteger͸ KotlinͰ͸Int!

  35. Javaͷίʔυ public class JInts { public static Integer getWrapperInteger() {

    return /*ུ*/; } }
  36. Kotlinͷίʔυ // num͸Platform TypeͷInt!ܕ val num = JInts.getWrapperInteger() ѻ͍ํΛؒҧ͑ͨΒIllegalStateExcep,onʹͳͬͨΓɺNullPointerExcep,onʹͳͬͨΓ

  37. ଞͷPla$orm(Typeͱಉ༷ʹ @Nullable΍@NotNullΛ࢖͓͏

  38. Javaͷίʔυ public class JInts { public static Integer getWrapperInteger() {

    return /*ུ*/; } @Nullable public static Integer getWrapperIntegerNullable() { return /*ུ*/; } @NotNull public static Integer getWrapperIntegerNotNull() { return /*ུ*/; } }
  39. Kotlinͷίʔυ // num͸Platform TypeͷInt!ܕ val num = JInts.getWrapperInteger() // num͸Int?ܕ

    val num = JInts.getWrapperIntegerNullable() // num͸Intܕ val num = JInts.getWrapperIntegerNotNull()
  40. ͳΔ΄Ͳ

  41. Ͱ͸ٯʹɺKotlinͷInt?΍Double?͸Java͔ΒͲ͏ݟ͑Δʁ

  42. KotlinͷInt? JavaͰ͸Interger

  43. Kotlinͷίʔυ class KInts { companion object { @JvmStatic fun getInt():

    Int = 0 @JvmStatic fun getNullableInt(): Int? = 0 } }
  44. Javaͷίʔυ public class KIntsUsage { public static void main(String[] args)

    { int num0 = KInts.getInt(); Integer num1 = KInts.getNullableInt(); // ΞϯϘΫγϯάͰ͜͏΋͔͚Δ int num2 = KInts.getNullableInt(); } }
  45. KotlinͷInt? JavaͰ͸Interger

  46. ͳΔ΄Ͳ

  47. Mapped&Type͸࣮͸primi,ve͚ͩ͡Όͳͯ͘ࢀরܕʹ΋͋Δ • java.lang.Enum,<.>,kotlin.Enum! • java.lang.Void,<.>,kotlin.Nothing! • java.lang.String,<.>,kotlin.String! • java.lang.Number,<.>,kotlin.Number! ࣮͸String΋ʂʂʂ

  48. ࠷ޙʹ

  49. ʰ(Mutable)List<T>!ʱͱ͸ʁ

  50. Mapped&TypeͱPla+orm&Typeͷ߹Θٕͤ

  51. Kotlinͷ 2ͭͷϦετΠϯλʔϑΣʔε

  52. kotlin.collec*ons.List<T> count()ͱ͔ཁૉऔಘͱ͔ReadOnlyͳϝιου͔͠ͳ͍ (ͪͳΈʹList<T>͸ΠϛϡʔλϒϧͰ͸ͳ͍)

  53. kotlin.collec*ons.MutableList<T> List<T>Λܧঝ͠͞ΒʹཁૉΛมߋ͢ΔϝιουΛ࣋ͭ (clearͱ͔addͱ͔Ͱ͖Δ)

  54. ͲͪΒ΋JavaͰ͸ java.u&l.List<T>

  55. Kotlinͷίʔυ class KLists { companion object { @JvmStatic fun getStringList():

    List<String> = listOf("") @JvmStatic fun getMutableStringList(): MutableList<String> = mutableListOf("") } }
  56. Javaͷίʔυ List<String> list0 = KLists.getStringList(); List<String> list1 = KLists.getMutableStringList();

  57. Ͱ͸Javaͷjava.u'l.List<String> Kotlin͔Β͸Ͳ͏ݟ͑Δ?

  58. Javaͷίʔυ public class JLists { public static List<String> getList() {

    return new ArrayList<>(); } }
  59. Kotlinͷίʔυ // IDEͰͷදࣔ͸ʰ(Mutable)List<String!>!ʱܕ val list = JLists.getList() println(list.count) // MutableListͷϝιου͕asͳ͠Ͱݺ΂Δ

    list.clear() println(list.count)
  60. MutableList͡Όͳͯ͘Listͱͯ͠ѻ͍͍ͨ৔߹͸ʁ

  61. @ReadOnly

  62. @ReadOnlyͱ@MutableΛ෇༩ͨ͠Javaͷίʔυ(@NotNull΋) public class JLists { @NotNull @ReadOnly public static List<String>

    getReadonlyList() { return new ArrayList<>(); } @NotNull @Mutable public static List<String> getMutableList() { return new ArrayList<>(); } }
  63. Kotlinͷίʔυ // kotlin.collections.List<String!> val list = JLists.getReadonlyList() // ͜Ε΋OK val

    list : List<String> = JLists.getReadonlyList() // ίϯύΠϧΤϥʔ val list : MutableList<String> = JLists.getReadonlyList() println(list.count()) // OK list.clear() // ίϯύΠϧΤϥʔ @ReadOnlyͱ@MutableΛ͚ͭΔ͜ͱͰมԽ
  64. // kotlin.collections.MutableList<String!> val list = JLists.getMutableList() // ͜Ε΋OK val list

    : MutableList<String> = JLists.getMutableList() println(list.count()) // OK list.clear() // OK // MutableList͸ListΛܧঝ͍ͯ͠ΔͷͰɺͭ͡͸͜Ε΋OK // ͨͩclearͱ͔add͸ݺ΂ͳ͍ // val list : List<String> = JLists.getMutableList() @ReadOnlyͱ@MutableΛ͚ͭΔ͜ͱͰมԽ
  65. ͜ΕΒ͸List<T>͚ͩͰͳ͘ Set<T>΍Map<T>Ͱ΋͍ͬ͠ΐ

  66. ·ͱΊ Pla$orm(TyoeͱMapped(Type ཧղ͓ͯ͜͠͏ʂ