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

Kotlin Types and compatibility with Java

Kotlin Types and compatibility with Java

きりみん

November 21, 2017
Tweet

More Decks by きりみん

Other Decks in Programming

Transcript

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

    = null // ίϯύΠϧΤϥʔ var str2: String? = “konichiwa” str2 = null str2.length // ίϯύΠϧΤϥʔ str2?.length
  2. Swiftͱಉ͘͡جຊతʹ Πϛϡʔλϒϧͳ஋Λ࢖༻͢Δ • ѻ͍ํ͸͍͍ͩͨಉ͡ var str1 = “konichiwa” str1 =

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

    “b”, “c”) list.add(“d”) // ίϯύΠϧΤϥʔʂ(add()΍remove()ϝιου͸࢖͑ͳ͍) val mList = mutableListOf(“a”, “b”, “c”) mList.add(“d”)
  4. 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; }
  5. ϓϥοτϑΥʔϜܕʁ • String!ͷΑ͏ʹදݱ͞ΕΔܕ • Nullڐ༰ܕ͔ඇڐ༰ܕ͔ෆ໌ͳঢ়ଶ • Ξϯϥοϓ͠ͳͯ͘΋ͦͷ··ࢀরͰ͖Δ • ࣮ͨͩ͠ߦ࣌ʹNullPointerException͕ൃੜ͠͏Δ •

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