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

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. SwiftΤϯδχΞʹ঺հ͢Δ KotlinͷܕͱJavaͱͷޓ׵ੑ Swift/Kotlin߹ಉษڧձ #2

  2. ࣗݾ঺հ

  3. ΘΓ͔ΜΛݱۚͰਫ਼ࢉ͠ͳͯ͘΋ޙ͔ΒεϚϗ্Ͱ੥ٻͨ͠Γग़དྷΔ ΞϓϦͩΑʂΘΓ͔Μ(ۉ౳ׂ)͡Όͳͯ͘΋࢖͑ΔΑʂϥϯνͱ͔ʂ Android൛͸΄΅KotlinͰ࣮૷͞Ε͍ͯΔΑʂ

  4. Kotlinʹ͍͓ͭͯ͞Β͍

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

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

    “hallo” val str2 = “konichiwa” str2 = “hallo” // ίϯύΠϧΤϥʔ
  7. ϓϦϛςΟϒܕͱࢀরܕ (ΦϒδΣΫτ)Λ۠ผ͠ͳ͍ • Javaʹ͸ϓϦϛςΟϒܕͱΦϒδΣΫτͱͯ͠ѻ ͏ͨΊͷϥούʔΫϥε͕͕͋ͬͨɺKotlinͰ͸۠ ผ͠ͳ͍ • int(Integer) → Int

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

    “b”, “c”) list.add(“d”) // ίϯύΠϧΤϥʔʂ(add()΍remove()ϝιου͸࢖͑ͳ͍) val mList = mutableListOf(“a”, “b”, “c”) mList.add(“d”)
  9. KotlinͷίϨΫγϣϯ֓ཁ $PMMFDUJPO .VUBCMF$PMMFDUJPO -JTU .VUBCMF-JTU

  10. ͱ͜ΖͰɺKotlin͸Javaͱ ׬શʹޓ׵ੑ͕͋Γ·͢

  11. None
  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; }
  13. Kotlin͔ΒݟͨJavaม਺ • JavaͰఆٛ͞Εͨ஋ΛKotlin͔Βࢀর͢Δͱϓ ϥοτϑΥʔϜܕͱ͍͏ಛघͳܕʹͳΔ public String getStr(String str) { return

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

    JavaଆͰ@Nullable΍@NotNull͕෇͍͍ͯΕ͹ਖ਼͍͠ܕʹม׵͞ΕΔ • ͪͳΈʹintͳͲͷϓϦϛςΟϒܕ͸nullʹͳΓ͑ͳ͍ͷͰৗʹඇڐ༰ ܕͱͯ͠ѻ͑Δ
  15. ͳͥϓϥοτϑΥʔϜܕʁ • ΋ͱ΋ͱ͸JavaͰఆٛ͞Εͨ஋͸͢΂ͯNullableܕ ͱͯ͠ѻ͏Ҋ΋͋ͬͨ • ͔ͦ͠͠ΕͰ͸JavaͷϥΠϒϥϦΛࢀরͨ͠৔߹ͳ Ͳʹίʔυ͕?ͩΒ͚ʹͳͬͯ͠·͍ݱ࣮తͰ͸ͳ͍ • ݱ࣮తͳརศੑΛॏࢹ͠ɺϓϥοτϑΥʔϜܕΛɹ ఆٛͨ͠

  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; }
  17. Java͔ΒݟͨKotlinίϨΫγϣϯ • JavaͷίϨΫγϣϯͱKotlinͷίϨΫγϣϯ͸ ޓ׵ੑ͕͋Γಛʹҙࣝ͢Δ͜ͱͳ͘ѻ͏ࣄ͕ Ͱ͖Δ • ͨͩ͠KotlinͰ͸ෆมͰએݴ͞Ε͍ͯΔίϨΫ γϣϯͰ΋Java͔Β͸มߋͰ͖ͯ͠·͏

  18. Kotlin͔ΒݟͨJavaίϨΫγϣϯ • JavaͷList͸Kotlin͔Β͸MutableͳListͱͯ͠ ͦͷ··ѻ͑Δ • ListΛฦ͢JavaͷϝιουΛoverride͢Δ৔߹ ͳͲ͸MutableͳListͱͯ͠΋Immutableͳ Listͱͯ͠΋ఆٛग़དྷΔ

  19. KotlinͷίϨΫγϣϯ֓ཁ $PMMFDUJPO .VUBCMF$PMMFDUJPO -JTU .VUBCMF-JTU "SSBZ-JTU

  20. Java͔ΒݟͨIntܕ • جຊతʹ͸intʹม׵͞ΕΔ • ͨͩ͠ɺInt?ܕ΍Array<Int>ܕ(഑ྻ)ͳͲ͸Φ ϒδΣΫτͱͯ͠ѻ͏ඞཁ͕͋ΔͨΊɺ Integerܕͱͯ͠ѻΘΕΔ

  21. Kotlin͔ΒݟͨϦςϥϧ • ٯʹKotlin͔ΒݟͨJavaͷϦςϥϧ͸IntͳͲ ͷKotlinͷΦϒδΣΫτͱͯ͠ѻΘΕΔ

  22. ·ͱΊ • Kotlin͸JavaͷΫϥεͱҧ࿨ײͳ͘ڞଘͰ͖Δ ࣄΛڧ͘ҙࣝͨ͠ݴޠ࢓༷ʹͳ͍ͬͯΔ • ݱ࣮తͳ։ൃݱ৔ͰͷརศੑΛॏࢹͨ͠ࣄ͕ Kotlin͕ਓؾݴޠʹͳͬͨཧ༝ͷҰ͔ͭ΋͠Ε ͳ͍

  23. Kotlinͷ࢓༷Λ΋ͬͱ஌Γͨ͘ͳͬ ͨΒʮKotlin in Actionʯ͕Φεεϝ ࠓճ࿩ͨ͠Α͏ͳ಺༰͕ͱͯ΋෼͔Γ΍͘͢ղ આ͞Ε͍ͯ·͢ʂ