Kotlin Types and compatibility with Java

D96c7e61d2f394f1d0af66945181a230?s=47 きりみん
November 21, 2017

Kotlin Types and compatibility with Java

D96c7e61d2f394f1d0af66945181a230?s=128

きりみん

November 21, 2017
Tweet

Transcript

  1. 5.

    Swiftͱಉ͘͡Nullڐ༰ܕͱ ඇڐ༰ܕ͕͋Δ • ѻ͍ํ͸͍͍ͩͨಉ͡ var str1: String = “hallo” str1

    = null // ίϯύΠϧΤϥʔ var str2: String? = “konichiwa” str2 = null str2.length // ίϯύΠϧΤϥʔ str2?.length
  2. 6.

    Swiftͱಉ͘͡جຊతʹ Πϛϡʔλϒϧͳ஋Λ࢖༻͢Δ • ѻ͍ํ͸͍͍ͩͨಉ͡ var str1 = “konichiwa” str1 =

    “hallo” val str2 = “konichiwa” str2 = “hallo” // ίϯύΠϧΤϥʔ
  3. 8.

    ίϨΫγϣϯ΋ෆมͱՄมʹ ෼͔Ε͍ͯΔ • σϑΥϧτͷίϨΫγϣϯ͸ෆม • ૢ࡞͍ͨ͠৔߹͸MutableListͳͲΛ࢖͏ val list = listOf(“a”,

    “b”, “c”) list.add(“d”) // ίϯύΠϧΤϥʔʂ(add()΍remove()ϝιου͸࢖͑ͳ͍) val mList = mutableListOf(“a”, “b”, “c”) mList.add(“d”)
  4. 11.
  5. 12.

    Java͔ΒݟͨNullඇڐ༰ܕ • KotlinͷNullඇڐ༰ܕ͸JavaͰ͸@NotNullΞ ϊςʔγϣϯʹͳΔ fun getStr(str: String): String { return

    str } ↓ @NotNull public String getStr(@NotNull String str) { Intrinsics.checkParameterIsNotNull(str, "str"); return str; }
  6. 14.

    ϓϥοτϑΥʔϜܕʁ • String!ͷΑ͏ʹදݱ͞ΕΔܕ • Nullڐ༰ܕ͔ඇڐ༰ܕ͔ෆ໌ͳঢ়ଶ • Ξϯϥοϓ͠ͳͯ͘΋ͦͷ··ࢀরͰ͖Δ • ࣮ͨͩ͠ߦ࣌ʹNullPointerException͕ൃੜ͠͏Δ •

    JavaଆͰ@Nullable΍@NotNull͕෇͍͍ͯΕ͹ਖ਼͍͠ܕʹม׵͞ΕΔ • ͪͳΈʹintͳͲͷϓϦϛςΟϒܕ͸nullʹͳΓ͑ͳ͍ͷͰৗʹඇڐ༰ ܕͱͯ͠ѻ͑Δ
  7. 16.

    Java͔ΒݟͨKotlinίϨΫγϣϯ • KotlinͷList΍MutableList͸Java͔Β͸ java.util.Listʹݟ͑Δ fun getList(list: List<String>): List<String> { return

    list } ↓ @NotNull public List getList(@NotNull List list) { Intrinsics.checkParameterIsNotNull(list, "list"); return list; }