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. 3.
  2. 4.
  3. 9.
  4. 11.

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

    val message : String = loadMessage() // Nullableܕ͸?.Ͱͳ͍ͱϝϯόΞΫηεͰ͖ͳ͍ // ΋͠message͕nullͳΒmessage?.length΋null println(message?.length) // ίϯύΠϧΤϥʔ println(message.length)
  5. 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()
  6. 19.
  7. 23.

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

    // ͦͷΦϒδΣΫτ͕nullͩͬͨΒNullPointerException println(message.length) // ͜ΕͳΒOKɻnullʹͳΔ println(message?.length)
  8. 38.

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

    return /*ུ*/; } @Nullable public static Integer getWrapperIntegerNullable() { return /*ུ*/; } @NotNull public static Integer getWrapperIntegerNotNull() { return /*ུ*/; } }
  9. 39.

    Kotlinͷίʔυ // num͸Platform TypeͷInt!ܕ val num = JInts.getWrapperInteger() // num͸Int?ܕ

    val num = JInts.getWrapperIntegerNullable() // num͸Intܕ val num = JInts.getWrapperIntegerNotNull()
  10. 40.
  11. 43.

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

    Int = 0 @JvmStatic fun getNullableInt(): Int? = 0 } }
  12. 44.

    Javaͷίʔυ public class KIntsUsage { public static void main(String[] args)

    { int num0 = KInts.getInt(); Integer num1 = KInts.getNullableInt(); // ΞϯϘΫγϯάͰ͜͏΋͔͚Δ int num2 = KInts.getNullableInt(); } }
  13. 46.
  14. 48.
  15. 55.

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

    List<String> = listOf("") @JvmStatic fun getMutableStringList(): MutableList<String> = mutableListOf("") } }
  16. 61.
  17. 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<>(); } }
  18. 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Λ͚ͭΔ͜ͱͰมԽ
  19. 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Λ͚ͭΔ͜ͱͰมԽ