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

Watashi ni Kotlin ga maiorita

Watashi ni Kotlin ga maiorita

きりみん

March 19, 2019
Tweet

More Decks by きりみん

Other Decks in Programming

Transcript

  1. ࢲʹKotlin͕෣͍߱Γͨ
    -AndroidͰͷKotlinҠߦΛݟ͖ͯͨࢹ఺͔ΒͷKotlin-
    in ΦλΫ͕KotlinΛ௥͏ϥΠτχϯάτʔΫΠϕϯτ
    @kirimin

    View full-size slide

  2. ࣗݾ঺հ
    • @kirimin
    • AndroidΤϯδχΞ
    • ݱࡏ͸KotlinͰAndroid։ൃ
    • Kotlinྺ5೥͘Β͍
    • ۀ຿ͰJava͔ΒKotlinҠߦܦݧ͋Γ

    View full-size slide

  3. ͖ΓΈΜͪΌΜ
    • όʔνϟϧ༮ঁϓϩάϥϚʔ͖ΓΈΜͪΌ
    ΜͷϚωʔδϟʔͰ͢ɻ
    • ͍ΘΏΔVTuberɻ
    • ϓϩάϥϛϯά΍ΤϯδχΞʹ͍ͭͯͷಈ
    ըΛ౤ߘͨ͠ΓϥΠϒίʔσΟϯά഑৴Λ
    ͨ͠Γͯ͠·͢ɻ
    • νϟϯωϧొ࿥ΑΖ͘͠Ͷʂ

    View full-size slide

  4. ٕज़ॻయͰຊΛग़͠·͢(એ఻)
    • ϑϦʔϥϯεͷ஌ݟΛ
    ·ͱΊͨຊͱɺ͖ΓΈ
    ΜͪΌΜͷϑΝϯϒο
    Ϋ
    • ٕज़ॻయ6͸4/14ʂ
    • ങ͍ʹ͖͍ͯͩ͘͞ʂ

    View full-size slide

  5. Kotlinͱࢲ
    • ·ͩKotlinͷ஌໊
    ౓͕΄ͱΜͲͳ͔ͬ
    ͨ2015೥ॳ಄͔
    ΒKotlinਪ͠
    • ΅͕͘ॳΊͯ
    Kotlinʹग़ձͬͨ
    ೔ͷπΠʔτ

    View full-size slide

  6. Kotlinͱࢲ
    • झຯͷΞϓϦΛKotlinʹϑϧॻ͖׵͑͢Δ
    (2015೥)
    • ࢓ࣄͰ΋KotlinҠߦΛܦݧ(2017೥)
    • ࠓ͸ϑϧKotlinͷϓϩδΣΫτͰ։ൃ(2018)

    View full-size slide

  7. Javaͱൺֱͨ͠Kotlinͷར఺

    View full-size slide

  8. Kotlinͷجૅ(ͬ͘͟Γ)
    • JVMͰಈ͘Java׬શޓ׵ݴޠ
    • ੜ࢈ੑͱཧղ͠΍͢͞ɺಋೖͷ͠΍͢͞Λҙࣝͨ͠ݱ࣮ࢤ޲ͷݴޠ
    • ؔ਺ܕͷΤοηϯε΍Ϟμϯͳݴޠ࢓༷Λ࠾༻ͨ͠Alt Java
    • Nullڐ༰ܕͱNullඇڐ༰ܕ
    • valʹΑΔఆ਺એݴ
    • JavaΑΓ΋ؾܰʹ͔ͭ͑ΔศརͳϦετૢ࡞ؔ਺

    View full-size slide

  9. JVMͰಈ͘Java׬શޓ׵ݴޠ
    • Kotlinͷιʔείʔυ͸JavaόΠτίʔυʹม׵͞ΕΔ
    • ͭ·Γ࠷ऴతʹJavaʹͳΔ
    • JavaͷΫϥε΍ϥΠϒϥϦ͕ී௨ʹ࢖͑Δ
    • JavaϓϩδΣΫτ಺ͰڞଘՄೳ
    • ٯʹKotlinͰॻ͍ͨίʔυΛJava͔Βݺͼग़͢ޓ׵ੑ΋༻ҙ
    ͞Ε͍ͯΔ

    View full-size slide

  10. ੜ࢈ੑͱཧղ͠΍͢͞ɺಋೖͷɹɹ
    ͠΍͢͞Λҙࣝͨ͠ݱ࣮ࢤ޲ͷݴޠ
    • Kotlin͸ಋೖͷ͠΍͢͞΍ֶशίετͷ௿͕͞ڧΈ
    • Ϟμϯ͚ͩͲγϯϓϧͳݴޠ࢓༷
    • ࠷৽ͷJavaܦݧऀ΍SwiftܦݧऀͳΒΩϟονΞοϓ͸೉͘͠ͳ͍
    • Scala΍ClojureͳͲʹର͢Δେ͖ͳڧΈ
    • GradleʹΑΔϏϧυγεςϜ
    • IntelliJͷެࣜαϙʔτ

    View full-size slide

  11. ؔ਺ܕͷΤοηϯε΍Ϟμϯͳ
    ݴޠ࢓༷Λ࠾༻ͨ͠Alt Java
    • ߴ֊ؔ਺(ؔ਺Λड͚औΔؔ਺)΍Πϛϡʔλϒϧ
    ͳͲؔ਺ݴޠతͳΤοηϯεΛऔΓೖΕ͍ͯΔ
    • Java΋࠷ۙ͸ϥϜμࣜ΍StreamɺOptionalɺϥ
    ϜμࣜͳͲؔ਺ܕతͳϞμϯͳݴޠ࢓༷ΛऔΓ
    ೖΕ͍ͯΔ͕ɺKotlin͸ݴޠϨϕϧͰαϙʔτ͞
    Ε͓ͯΓɺΑΓચ࿅͞Ε͍ͯΔ

    View full-size slide

  12. Nullڐ༰ܕͱNullඇڐ༰ܕ
    • KotlinͰ͸ม਺͸جຊnullΛڐ༰͠ͳ͍ɻ
    • nullΛڐ༰͢Δ৔߹͸໌ࣔతʹ?Λ෇͚ܕએݴ
    ͢Δඞཁ͕͋Δ
    • JavaͷOptionalͱಉ͡ߟ͑ํ͕ͩɺͪ͜Β͸
    ݴޠ࢓༷ͱͯ͠αϙʔτ

    View full-size slide

  13. Nullڐ༰ܕͱNullඇڐ༰ܕ
    var hoge: String = null // ίϯύΠϧΤϥʔ
    var hoge: String = "hoge"
    hoge = null // ίϯύΠϧΤϥʔ
    --------------------------------------------
    var fuga: String? = null // OK
    var fuga: String? = "fuga"
    fuga = null // OK

    View full-size slide

  14. NullΛѻ͏ͨΊͷศརͳؔ਺
    var hoge: String? = "hoge"
    hoge.size // ίϯύΠϧΤϥʔ
    hoge?.size // hoge͕nullͳΒ͹࣮ߦ͞Εͳ͍
    hoge?.size ?: return // hoge͕nullͳΒreturn
    val size = hoge?.size ?: 0 // hoge͕nullͳΒ0Λ୅ೖ
    hoge?.let { // hoge͕nullͳΒ࣮ߦ͞Εͳ͍
    println(it.size)
    }
    hoge.isNullOrEmpty() // null΋͘͠͸ۭจࣈͳΒtrue

    View full-size slide

  15. valએݴͱvarએݴ
    • valͰએݴͨ͠ม਺͸ෆม(ఆ਺)ʹͳΔ
    • var͸Մม
    • KotlinͰ͸ग़དྷΔ͚ͩvalΛ࢖͏จԽ

    View full-size slide

  16. valએݴͱvarએݴ
    val hoge: String = "hoge"
    hoge = "fuga" // ίϯύΠϧΤϥʔ
    --------------------------------------------
    var hoge: String = "hoge"
    hoge = "fuga" // OK

    View full-size slide

  17. JavaΑΓ΋ؾܰʹ͔ͭ͑Δศརͳ
    Ϧετૢ࡞ؔ਺
    • KotlinͷCollectionΠϯλʔϑΣʔεʹ͸map
    ΍filterͱ͍ͬͨͨ͘͞Μͷؔ਺ܕతͳศརϝ
    ιου͕ఆٛ͞Ε͍ͯΔ
    • JavaͷStreamͷΑ͏ͳ΋ͷ͕ͩɺΑΓؾܰʹ
    forจͳͲͷ୅ΘΓʹ࢖͑Δ

    View full-size slide

  18. JavaΑΓ΋ؾܰʹ͔ͭ͑Δศརͳ
    Ϧετૢ࡞ؔ਺
    val list = listof("a", "bb", "ccc")
    val sizeList = list.map { str -> str.size }
    // sizeList = { 1, 2, 3 }
    val filteredList = list.filter { str -> str != "bb" }
    // filteredList = { "a", "ccc" }

    View full-size slide

  19. Java͔ΒKotlin΁ͷҠߦ

    View full-size slide

  20. Java͔ΒKotlin΁ͷҠߦ
    • KotlinͱJava͸ڞੜͰ͖Δ
    • 1Ϋϥε͔ΒҠߦΛ࢝ΊΒΕΔ
    • (࠶)JavaͷϥΠϒϥϦ΍ϑϨʔϜϫʔΫ͸جຊKotlin
    Ͱ΋࢖͑Δ
    • IntelliJʹϑΝΠϧΛJava͔ΒKotlinʹࣗಈม׵ͯ͠
    ͘ΕΔػೳ͕͋Δ

    View full-size slide

  21. Java͔ΒKotlin΁Ҡߦͯ͠ਏ͔ͬ
    ͨ࿩
    • AndroidͰ͸΄ͱΜͲฉ͍ͨ͜ͱ͕ͳ͍
    • ֶशίετ͸௿͘Javaͱൺ΂ϋʔυϧ͕ߴ͍ͱ͍͏
    ͜ͱ͸͋·Γͳ͍
    • KotlinͱJava͕ڞଘ͍ͯ͠ΔϓϩδΣΫτ΋ͨ͘͞
    Μ͋Δ
    • Ή͠ΖҰ౓Kotlinʹ׳ΕΔͱJavaʹ͸໭Εͳ͘ͳΔ

    View full-size slide

  22. ·ͱΊ
    • Kotlin͸ݱ࣮ࢤ޲ָ͔͍ͭ͠ݴޠ
    • AndroidͷੈքͰKotlinҠߦ͸΋ͷ͍͢͝੎͍
    ͰਐΜͰ͍Δ
    • όοΫΤϯυͷੈքͰ΋Kotlin͕ελϯμʔυ
    ʹͳΔϙςϯγϟϧ͸ଟ͍ʹ͋Δͱࢥ͍ͬͯΔ

    View full-size slide

  23. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ

    View full-size slide