How to start Kotlin APP DOJO

How to start Kotlin APP DOJO

今から始めるKotlinによるAndroidアプリ開発
2017/08/29 APP DOJO August 2017 @ Google Japan

B0f8177e3259f29952640f98bf795965?s=128

satorufujiwara

August 29, 2017
Tweet

Transcript

  1. ࠓ͔Β࢝ΊΔKotlinʹΑΔAndroidΞϓϦ։ൃ 2017.08.29 APP_DOJO @satorufujiwara

  2. About me Satoru Fujiwara (@satorufujiwara) Android Engineer @CyberAgent ,Inc Shibuya.apk

  3. Kotlin & Me FRESH! ΛKotlinͰ։ൃ (2015೥3݄ʙ) Kotlin Advent Calendar (2015/2016)

    -ʮ2016೥ɺKotlinͰAndroid։ൃ͢Δํ΁ʯ @Qiita ( goo.gl/0medax ) APP DOJO ʮKotlin in Google I/O 2017ʯ(2017/05/25) Kotlinೖ໳·Ͱͷॿ૸ಡຊ ( goo.gl/5vUT7o )
  4. Kotlinೖ໳·Ͱͷॿ૸ಡຊ • ೔ຊKotlinϢʔβάϧʔϓͷ໊લͰ༗ࢤ 9໊Ͱࣥච • Google I/O 2017ͰKotlinͷΦϑΟγϟ ϧԽ͕ൃද͞Εͨޙɺ1िؒͰॻ͍ͨ •

    ແྉͰެ։ ( goo.gl/5vUT7o )
  5. “ͱ͸ݴͬͯ΋ɺࠓճͷൃදͰ͸͡Ίͯ Kotlin ͱ͍͏ϓϩάϥϛ ϯάݴޠΛࣖʹͨ͠։ൃऀ΋ଟ͍͘Δ͜ͱͰ͠ΐ͏ɻ ʮKotlin ͱ͸Կͩ?ʯͱ͔ʮͳΜͰ Kotlinͳͷ͔?ʯͱ͔ʮͲ͏ ΍ֶͬͯ΂͹͍͍ͷ͔?ʯͱ͍͏ށ࿭͍΋͋Δ͔ͱࢥ͍·͢ɻ ͦ͜Ͱ… “

    Kotlinೖ໳·Ͱͷॿ૸ಡຊɺલॻ͖ΑΓ
  6. ຊ೔ͷGoal • ʮKotlin ͱ͸Կͩʁʯ • ʮͳΜͰ Kotlinͳͷ͔ʁʯ • ʮͲ͏΍ֶͬͯ΂͸ྑ͍ͷ͔ʁʯ

  7. ຊ೔ͷGoal • ʮKotlin ͱ͸Կͩʁʯˠ Kotlinͷߟ͑ํʹ͍ͭͯ஌Δ • ʮͳΜͰ Kotlinͳͷ͔ʁʯˠ KotlinͷݴޠػೳΛ஌Δ •

    ʮͲ͏΍ֶͬͯ΂͸ྑ͍ͷ͔ʁʯˠ KotlinΛ࢝ΊΔͨΊͷπʔϧΛ஌Δ
  8. Kotlinͷߟ͑ํ

  9. Kotlin in Action • Kotlinͷ։ൃνʔϜʹΑͬͯॻ͔Εͨຊ • Kotlinͷίϯηϓτ͔ΒKotlinͷݴޠػೳΛجຊ ػೳ(if/when/ΫϥεͳͲ)͔Βൃలػೳ(nullable/ lambda/ߴ֊ؔ਺/૯শܕ)·ͰΧόʔ •

    ࠓ೔ͷϓϨθϯͷલ൒͸͜ͷຊͷ಺༰ʹ͔ͳΓԊͬ ͍ͯ·͢
  10. Kotlin in Action • ຋༁ຊग़·͢ʂ • ༁ऀ • ௕ᖒଠ࿠ @ngsw_taro

    • ࢁຊ७ฏ @boohbah • yy_yank @yy_yank • ౻ݪ੟ @satorufujiwara
  11. Kotlin ͕໨ࢦ͍ͯ͠Δͱ͜Ζ • ݱࡏ Java ͕࢖Θ͍ͯΔ؀ڥʹɺΑΓ؆ܿ ͰɺΑΓੜ࢈ੑ͕ߴ͘ɺΑΓ ҆શͳ Java ͷ୅ସݴޠΛఏڙ͢Δ͜ͱ

    • ݱঢ়ͩͱAndroidͱαʔόʔαΠυ • কདྷతʹ͸JavaScript؀ڥ΍ɺଞͷ؀ڥ(iOSͳͲ)ʹ΋ରԠ
  12. Kotlin ͕໨ࢦ͍ͯ͠Δͱ͜Ζ • ݱࡏ Java ͕࢖Θ͍ͯΔ؀ڥʹɺΑΓ؆ܿ ͰɺΑΓੜ࢈ੑ͕ߴ͘ɺΑΓ ҆શͳ Java ͷ୅ସݴޠΛఏڙ͢Δ͜ͱ

    • ݱঢ়ͩͱAndroidͱαʔόʔαΠυ • কདྷతʹ͸JavaScript؀ڥ΍ɺଞͷ؀ڥ(iOSͳͲ)ʹ΋ରԠ
  13. Kotlin ͷߟ͑ํ • ݱࡏJava͕࢖ΘΕ͍ͯΔ؀ڥͰɺΑΓੜ࢈ੑ͕ߴ͘ • ΑΓ؆ܿͰ • ΑΓ҆શͰ • Javaͷ୅ସݴޠ

  14. Kotlin ͷߟ͑ํ • ݱࡏJava͕࢖ΘΕ͍ͯΔ؀ڥͰɺΑΓੜ࢈ੑ͕ߴ͘ → ࣮༻ओٛ • ΑΓ؆ܿͰ • ΑΓ҆શͰ

    • Javaͷ୅ସݴޠ
  15. Kotlin ͷߟ͑ํ • ݱࡏJava͕࢖ΘΕ͍ͯΔ؀ڥͰɺΑΓੜ࢈ੑ͕ߴ͘ → ࣮༻ओٛ • ΑΓ؆ܿͰ → ؆ܿ

    • ΑΓ҆શͰ • Javaͷ୅ସݴޠ
  16. Kotlin ͷߟ͑ํ • ݱࡏJava͕࢖ΘΕ͍ͯΔ؀ڥͰɺΑΓੜ࢈ੑ͕ߴ͘ → ࣮༻ओٛ • ΑΓ؆ܿͰ → ؆ܿ

    • ΑΓ҆શͰ → ҆શ • Javaͷ୅ସݴޠ
  17. Kotlin ͷߟ͑ํ • ݱࡏJava͕࢖ΘΕ͍ͯΔ؀ڥͰɺΑΓੜ࢈ੑ͕ߴ͘ → ࣮༻ओٛ • ΑΓ؆ܿͰ → ؆ܿ

    • ΑΓ҆શͰ → ҆શ • Javaͷ୅ସݴޠ → ૬ޓӡ༻ੑ
  18. Kotlin ͷߟ͑ํ • ࣮༻ओٛ • ؆ܿ • ҆શ • ૬ޓӡ༻ੑ

  19. Kotlin ͷߟ͑ํ • ࣮༻ओٛ • ؆ܿ • ҆શ • ૬ޓӡ༻ੑ

  20. Kotlin ͷ࣮༻ओٛ • Javaͷߟ͑ํ(ΦϒδΣΫτࢦ޲ͳͲ)͕جຊͦͷ··࢖͑Δ • ৽͍͠ػೳ(Lambda/Nullable etc)΋Ͳ͔͜Ͱฉ͍ͨ͜ͱ͋Δػೳ͹͔Γ • πʔϧ͕ॆ࣮͍ͯ͠Δ

  21. Kotlin ͷ࣮༻ओٛ • Javaͷߟ͑ํ(ΦϒδΣΫτࢦ޲ͳͲ)͕جຊͦͷ··࢖͑Δ • ৽͍͠ػೳ(Lambda/Nullable etc)΋Ͳ͔͜Ͱฉ͍ͨ͜ͱ͋Δػೳ͹͔Γ • πʔϧ͕ॆ࣮͍ͯ͠Δ

  22. data class Person(val name: String, val age: Int) fun findTheOldest(people:

    List<Person>): Person { var maxAge = 0 var theOldest: Person? = null for (person in people) { if (person.age > maxAge) { maxAge = person.age theOldest = person } } return theOldest }
  23. data class Person(val name: String, val age: Int) fun findTheOldest(people:

    List<Person>): Person { var maxAge = 0 var theOldest: Person? = null for (person in people) { if (person.age > maxAge) { maxAge = person.age theOldest = person } } return theOldest } fun findTheOldest(people: List<Person>) = persons.maxBy { it.age }
  24. Kotlin ͷ࣮༻ओٛ • Javaͷߟ͑ํ(ΦϒδΣΫτࢦ޲ͳͲ)͕جຊͦͷ··࢖͑Δ • Javaͷॻ͖ํ͕(ଟগҧ͏ͱ͜Ζ͸͋Δʹ͠Ζ)ͦͷ··࢖͑Δ • KotlinͰ௥Ճ͞Εͨؔ਺ܕϓϩάϥϛϯάͷܗͰ΋ॻ͚Δ • ͲͬͪͰॻ͔͘͸։ൃऀ࣍ୈ

  25. Kotlin ͷؔ਺ܕϓϩάϥϛϯά • ؔ਺Λ஋ͱͯ͠ѻ͑Δ • ϥϜμࣜ • σʔλΫϥε(ෆมͳ஋) • ඪ४ϥΠϒϥϦ಺ͷ๛෋ͳAPI܈(֦ுؔ਺)

  26. data class Person(val name: String, val age: Int) fun findTheOldest(people:

    List<Person>) = persons.maxBy{ it.age } σʔλΫϥε ϥϜμࣜ ؔ਺ ϥϜμ ΛҾ਺ʹऔΔ֦ுؔ਺ ෆม
  27. Kotlin ͷ࣮༻ओٛ • Javaͷߟ͑ํ(ΦϒδΣΫτࢦ޲ɾ੩తܕ෇͚)͕جຊͦͷ··࢖͑Δ • ؔ਺ܕϓϩάϥϛϯάͰॻ͘ͱɺศརͳ৔໘(ίϨΫγϣϯͳͲ)͸ଟ͍ • ͲͬͪͰॻ͔͘͸ڧ੍͞Ε͓ͯΒͣ։ൃऀ࣍ୈ

  28. Kotlin ͷߟ͑ํ • ࣮༻ओٛ • ؆ܿ • ҆શ • ૬ޓӡ༻ੑ

  29. Kotlin ͷ؆ܿ͞ • ಡΈ΍͢͞Λॏࢹ • ϘΠϥϓϨʔτ(͓ܾ·Γͷίʔυ=ಡ·ͳͯ͘ྑ͍ίʔυ)ΛۃྗݮΒ͢ • Α͋͘Δػೳ͸ඪ४ϥΠϒϥϦͰఏڙ(લड़ͷmaxByͳͲ) • ಡΈʹ͘͘ͳΔ͜ͱ͸ͤ͞ͳ͍(ΧελϜԋࢉࢠͳͲ)

  30. //Kotlin data class Person(val name: String, val age: Int)

  31. //Java public final class Person { private final String name;

    private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public final String getName() { return this.name; } public final int getAge() { return this.age; } public final Person copy(String name, int age) { return new Person(name, age); } public String toString() { return "Person(name=" + this.name + ", age=" + this.age + ")"; } public int hashCode() { return (this.name != null?this.name.hashCode():0) * 31 + this.age; } public boolean equals(Object var1) { //… } }
  32. Data class ϘΠϥϓϨʔτ(͓ܾ·Γͷίʔυ=ಡ·ͳͯ͘ྑ͍ίʔυ)ΛۃྗݮΒ͢ • ίϯύΠϥͰͷϑΟʔϧυ΁ͷ୅ೖ • getter / setter →

    ʮϓϩύςΟʯͱ͍͏ߟ͑ํ • copy / toString / hasCode / equals ΛࣗಈͰੜ੒
  33. Data class ΫϥεΛఆٛ͢Δ্Ͱͷؔ৺ࣄ͸ ʮϓϩύςΟͷܕ͕ԿͰɺ໊લ͕Կ͔ɺͦΕ͕Մม͔ෆม͔ʯ͚ͩ ͦΕҎ֎ͷϘΠϥʔϓϨʔτ͸ίϯύΠϥʹΑͬͯੜ੒͞ΕΔ data class Person(val name: String,

    val age: Int)
  34. Kotlinͷ؆ܿ͞ ੩తܕ෇͚ݴޠͰ͋Γɺܕਪ࿦͕ಇ͘ var maxAge = 0 var maxAge : Int

    = 0
  35. Kotlinͷ؆ܿ͞ ؔ਺ͷ໭Γ஋ͳͲ΋ܕਪ࿦͕ಇ͘ ؔ਺ͷදݱʹϥϜμ͕࢖͑Δ Α͋͘Δػೳ͸ඪ४ϥΠϒϥϦͰఏڙ fun findTheOldest(people: List<Person>) = persons.maxBy{ it.age

    } fun findTheOldest(people: List<Person>) : Person = persons.maxBy({ it.age })
  36. Kotlin ͷߟ͑ํ • ࣮༻ओٛ • ؆ܿ • ҆શ • ૬ޓӡ༻ੑ

  37. Kotlin ͷ҆શ͞ • ҆શੑΛߴΊΔ͜ͱͱ࣮૷ίετ͕ߴ͘ͳΔ͜ͱ͸ৗʹτϨʔυΦϑ • ΑΓ௿ίετͰߴ͍҆શੑΛ௥ٻ • ੩తܕ෇͚͸ͦͷ··ʹɺܕਪ࿦ʹΑΓίετΛԼ͍͛ͯΔ

  38. Kotlin ͷ҆શ͞ • ҆શੑΛߴΊΔ͜ͱͱ࣮૷ίετ͕ߴ͘ͳΔ͜ͱ͸ৗʹτϨʔυΦϑ • ΑΓ௿ίετͰߴ͍҆શੑΛ௥ٻ • ੩తܕ෇͚͸ͦͷ··ʹɺܕਪ࿦ʹΑΓίετΛԼ͍͛ͯΔ • ॏཁͳ2ͭͷΤϥʔΛίετͷΑΓ͔͔Βͳ͍ํ๏Ͱ๷ࢭ

    • NullPointerException & ClassCastException
  39. Null҆શ NullPointerException͕ϥϯλΠϜ࣌ʹى͜ΔͷΛ๷͙ Nullڐ༰ܕ(Nullable)ͱɺNullඇڐ༰ܕ(Non-Null)Λ۠ผ ௿ίετ(“?”Ұจࣈ)Ͱ࣮ݱ var theOldest: Person? = null var

    theOldest: Person = null //Error!
  40. ClassͷΩϟετ Ұ౓ܕνΣοΫͨ͠ޙͷείʔϓ಺Ͱ͸ɺ࠶౓ͷΩϟετ͸ෆཁ ʮεϚʔτΩϟετʯͱݺ͹ΕΔ if(value is String) { println(value.toUpperCase()) }

  41. Kotlin ͷߟ͑ํ • ࣮༻ओٛ • ؆ܿ • ҆શ • ૬ޓӡ༻ੑ

  42. Kotlin ͷ૬ޓӡ༻ੑ • Kotlin͸JavaͷclassϑΝΠϧ΁ͱίϯύΠϧ͞ΕΔ • KotlinͱJavaͷιʔεΛࣗ༝ʹߦ͖དྷͰ͖Δ • JavaͷϥΠϒϥϦΛ࢖͑ΔɺJava͔ΒKotlinͷίʔυΛ࢖͑Δ • Kotlin͸JavaͷطଘͷΫϥε(List΍MapͳͲ)Λ࢖͍ͬͯΔ

  43. Kotlin ͷߟ͑ํ • ࣮༻ओٛ - Javaͷߟ͑ํ͕ͦͷ··࢖͑Δɻ৽͍͠ػೳ͸طଘͷ΋ͷΛར ༻ɻֶश͕༰қɻ • ؆ܿ -

    ಡΈ΍͢͞ΛॏࢹɻϘΠϥʔϓϨʔτ͸ۃྗݮΒ͢ɻ • ҆શ - ੩తܕ෇͚ɻNull҆શɻεϚʔτΩϟετɻ • ૬ޓӡ༻ੑ - JavaͱKotlinͷؒ͸ࣗ༝ʹߦ͖དྷɻϥΠϒϥϦ΋طଘͷ΋ͷ Λͦͷ··ར༻Ͱ͖Δɻ
  44. Kotlin ͷ஫ҙ఺ • Javaͷ୅ସݴޠͰ͋Γ࣮༻ओٛͰ͋ΔͷͰɺࣗ༝౓͕ߴ͍ • ؆ܿʹॻ͖͗͢ΔͱಡΈʹ͘͘ͳΔ͜ͱ΋ • νʔϜ಺ͳͲͰͷίʔσΟϯάϧʔϧ͸߹ͬͨ΄͏͕ྑͦ͞͏

  45. Kotlinͷݴޠػೳ

  46. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  47. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  48. ม਺એݴ(val / var) & ܕਪ࿦ ෆม(val) or Մม(var) Ͱ͋Δ͔Λࢦఆͯ͠ม਺Λએݴ ܕ͸ม਺ͷޙΖʹஔ͕͘ܕਪ࿦ग़དྷΔ৔߹͸লུՄೳ

    ෆม(val) ͸Javaͷfinalʹ૬౰͠ɺ࠶୅ೖͰ͖ͳ͍ Մม - mutable ,ෆม - immutable val answer = 42 var answer2 : Int = 42 answer = 4 //Error! answer2 = 4 //OK!
  49. ม਺એݴ(val / var) & ܕਪ࿦ จ຺తʹҰ౓͔͠ॳظԽ͞Εͳ͍৔߹΋val͕࢖༻Ͱ͖Δ val message: String if

    (canPerformOperation()) { message = "Success" // ... perform the operation } else { message = "Failed" }
  50. ม਺એݴ(val / var) & ܕਪ࿦ • Ͱ͖Δ͚ͩෆม(val) Λ࢖͍ɺඞཁͳ࣌ʹ͚ͩՄม(var)ʹ͢Δ • ActivityͷϑΟʔϧυͳͲɺvalʹ͠ਏ͍৔ॴ΋͋Γ·͕͢ɺ஗ԆॳظԽ

    (lazy)ͳͲΛ࢖͑͹ՄೳʹͳΔ৔໘΋͋Δ • ܕ͸໌ࣔͯ͠΋ܕਪ࿦ʹ·͔ͤͯ΋ྑ͍
  51. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  52. Data class ίϯύΠϥͰͷϑΟʔϧυ΁ͷ୅ೖ getter / setter → ʮϓϩύςΟʯͱ͍͏ߟ͑ํ copy /

    toString / hasCode / equals ΛࣗಈͰੜ੒ data class Person(val name: String, var isMarried: Boolean)
  53. ௨ৗͷclass ίϯύΠϥͰͷϑΟʔϧυ΁ͷ୅ೖ getter / setter → ʮϓϩύςΟʯͱ͍͏ߟ͑ํ class Person(val name:

    String, var isMarried: Boolean)
  54. ௨ৗͷclass ίϯύΠϥͰͷϑΟʔϧυ΁ͷ୅ೖ getter / setter → ʮϓϩύςΟʯͱ͍͏ߟ͑ํ class Person(val name:

    String, var isMarried: Boolean)
  55. //Kotlin data class Person(val name: String, var isMarried: Boolean) //Kotlin

    >>> val person = Person("Bob", true) >>> println(person.name) Bob >>> println(person.isMarried) true
  56. //Kotlin data class Person(val name: String, var isMarried: Boolean) //Java

    >>> Person person = new Person("Bob", true); >>> System.out.println(person.getName()); Bob >>> System.out.println(person.isMarried()); true
  57. //Kotlin data class Person(val name: String, var isMarried: Boolean) //Java

    >>> Person person = new Person("Bob", true); >>> System.out.println(person.getName()); Bob >>> System.out.println(person.isMarried()); true person.setName("Alice") //Error! person.setMarried(false) //OK!
  58. //Kotlin data class Person(val name: String, var isMarried: Boolean) //Kotlin

    >>> val person = Person("Bob", true) >>> println(person.name) Bob >>> println(person.isMarried) true person.name = "Alice" //Error! person.isMarried = false //OK!
  59. ϓϩύςΟ • JavaͷϑΟʔϧυͱΞΫηαϝιου(setter/getter)Λஔ͖׵͑Δ΋ͷ • val nameͱએݴ͢ΔͱKotlin͔Β͸nameɺJava͔Β͸getNameͱͯ͠ ݟ͑Δ • ܕ͕Booleanͷ৔߹͸ྫ֎ͱͯ͠isͰ࢝·Δؔ਺ʹͳΔ •

    varͰએݴ͢Ε͹setter͕͋ΓɺvalͰએݴͨ͠৔߹͸setter͕ͳ͍
  60. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / while) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  61. ੍ޚߏจ • if - Javaͱ΄΅ಉ͡ • when - JavaͷswitchΛڧྗʹͨ͠΋ͷ(Kotlinʹswitchจ͸ແ͍) •

    for - Javaͷfor-each(‘for (String item : list )’ͳͲ)͔͠ͳ͍ • while - Javaͱશ͘ಉ͡
  62. ifจ KotlinͰ͸ifจΛࣜͱͯ͠ѻ͍ɺ஋Λฦ͢͜ͱ͕ग़དྷΔ Javaͷࡾ߲ԋࢉࢠ͸Kotlinʹ͸ແ͍ val larger = if(a > b) a

    else b
  63. ifจ தׅހΛ࢖͏৔߹͸ɺͦͷதͷ࠷ޙͷจΛif͕ฦ͢஋ͱͯ͠ѻ͏ ifҎ֎ʹɺwhen΍try-catch΋ಉ༷ʹ஋Λฦࣜ͢ͱͯ͠ѻ͑Δ val result : String = if(isSuccess()){ //

    do something "Success" } else { "Failure" }
  64. whenจ ڧྗͳswitchจ Javaͱҧ͍ɺϓϦϛςΟϒܕɺenumܕҎ֎ʹ΋࢖͑Δ fun getColorName(color: Color) = when (color) {

    Color.RED -> "Red" Color.ORANGE -> "Orange" Color.YELLOW -> "Yellow" else -> "Unknown" }
  65. whenจ ৚݅ʹෳ਺ͷ߲໨ΛೖΕΔ͜ͱ͕Մೳ fun getWarmth(color: Color) = when (color) { RED,

    ORANGE, YELLOW -> "warm" GREEN -> "neutral" BLUE, INDIGO, VIOLET -> "cold" }
  66. whenจ whenΛҾ਺ͳ͠Ͱ࢖͏͜ͱ΋ग़དྷΔ when { x.isOdd() -> print("x is odd") x.isEven()

    -> print("x is even") else -> print("x is funny") }
  67. forจ inΛ࢖ͬͯɺJavaͷfor-eachจͷΑ͏ͳ܁Γฦ͠ॲཧ͕ग़དྷΔ Collection΍MapΛinͷޙΖʹஔ͘͜ͱ͕ग़དྷΔ for (item in collection) print(item) for (i

    in 1..100) { print(fizzBuzz(i)) }
  68. forจ downTo΍stepΛ࢖ͬͯɺൣғΛࢦఆ͢Δ͜ͱ΋ग़དྷΔ for (i in 100 downTo 1 step 2)

    { print(fizzBuzz(i)) }
  69. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  70. Nullڐ༰ܕ / Nullඇڐ༰ܕ Nullڐ༰ܕ(Nullable)ͱɺNullඇڐ༰ܕ(Non-Null)Λ۠ผ ௨ৗͷܕ͸Nullඇڐ༰ܕɺͦΕʹʹ?͕͍ͭͨ΋ͷ͕Nullڐ༰ܕ ϓϦϛςΟϒܕʹ΋nullڐ༰ܕ͕͋Δ var a: String =

    "" var b: String? = null var c: Int? = null
  71. Nullඇڐ༰ܕ Nullඇڐ༰ܕ͸͜Ε·Ͱ௨ΓϓϩύςΟࢀরɺϝιουݺͼग़͕͠ग़དྷΔ Nullඇڐ༰ܕͷม਺ʹnullΛ୅ೖ͢ΔͱίϯύΠϧΤϥʔͱͳΔ fun strLength(str : String) = str.length strLength(null)

    // Error!
  72. Nullڐ༰ܕ Nullڐ༰ܕ͸ϓϩύςΟࢀরɺϝιουݺͼग़͕͠ίϯύΠϧΤϥʔͱͳ Δ → ϓϩύςΟࢀরɺϝιουݺͼग़࣌͠͸ಛผͳํ๏͕ඞཁ Nullڐ༰ܕʹ͸nullΛ୅ೖ͢Δ͜ͱ͕ग़དྷΔ fun strLength(str : String?)

    = str.length //Error! strLength(null) //OK!
  73. Nullڐ༰ܕ / Nullඇڐ༰ܕ • Ͱ͖Δ͚ͩNullඇڐ༰ܕ(Non-Null)Ͱఆٛ͢Δ • ඞཁͳ৔໘͚ͩʹNullڐ༰ܕ(Nullable)Λ࢖͏ • Nullڐ༰ܕ͸ಛผʹѻ͏ඞཁ͕͋Δ •

    Kotlin಺Ͱ͸ඞͣͲͪΒ͔ͷܕʹͳΔ͕ɺϓϥοτϑΥʔϜܕ(ܕ໊ͷޙΖʹ!)ͱ͍ ͏Java͔Β΍ͬͯ͘Δಛผͳܕ͕͋Δ • ίϯύΠϧޙ͸Javaͷ௨ৗͷܕͱಉ༷ʹѻΘΕΔͷͰϥϯλΠϜ࣌ͷΦʔόʔϔο υ͸ͳ͍
  74. Nullڐ༰ܕ / Nullඇڐ༰ܕ • Ͱ͖Δ͚ͩNullඇڐ༰ܕ(Non-Null)Ͱఆٛ͢Δ • ඞཁͳ৔໘͚ͩʹNullڐ༰ܕ(Nullable)Λ࢖͏ • Nullڐ༰ܕ͸ಛผʹѻ͏ඞཁ͕͋Δ •

    Kotlin಺Ͱ͸ඞͣͲͪΒ͔ͷܕʹͳΔ͕ɺϓϥοτϑΥʔϜܕ(ܕ໊ͷޙΖʹ!)ͱ͍ ͏Java͔Β΍ͬͯ͘Δಛผͳܕ͕͋Δ • ίϯύΠϧޙ͸Javaͷ௨ৗͷܕͱಉ༷ʹѻΘΕΔͷͰϥϯλΠϜ࣌ͷΦʔόʔϔο υ͸ͳ͍
  75. Nullඇڐ༰ܕͷѻ͍ํ • ifจʹΑΔnullνΣοΫ • ҆શݺͼग़͠ (?.) • ΤϧϏεԋࢉࢠ(?:) • ඇNullද໌(!!)

  76. ifจʹΑΔnullνΣοΫ ifจͰҰ౓ɺnullͰͳ͍͜ͱΛνΣοΫ͢ΔͱnullʹͳΓಘͳ͍จ຺ͷதͰ ͸ɺNullඇڐ༰ܕͱͯ͠ѻ͑Δ ࢓૊Έతʹ͸εϚʔτΩϟετ fun strLength(str : String?) : Int

    = if (str != null) { str.length } else { 0 }
  77. ҆શݺͼग़͠(?.) Nullڐ༰ܕʹ?.Λ͚ͭΕ͹ɺϓϩύςΟ΍ϝιουΛ҆શʹݺͼग़ͤΔ ม਺͕nullͷ৔߹͸ݺͼग़͕࣮͠ߦ͞ΕͣɺnullΛฦ͢ νΣΠϯͤ͞Δ͜ͱ΋ग़དྷΔ fun strLength(str : String?) : Int?

    = str?.length >>> println(strLength("abc")) 3 >>> println(strLength(null)) null /VMMڐ༰ܕ
  78. ΤϧϏεԋࢉࢠ(?:) ԋࢉࢠ(?:)ͷલ͕nullͳΒޙΖͷ஋Λฦ͢ ΤϧϏεɾϓϨεϦʔ໊͕લͷ༝དྷ fun strLength(str : String?) : Int =

    str?.length ?: 0 >>> println(strLength("abc")) 3 >>> println(strLength(null)) 0 /VMMඇڐ༰ܕ
  79. ඇNullද໌(!!) Nullڐ༰ܕΛNullඇڐ༰ܕʹڧ੍తʹม׵͢Δ ΋͠null͕ೖͬͯͨ৔߹͸ϥϯλΠϜ࣌ʹNullPointerException͕౤͛ΒΕ Δ fun strLength(str : String?) : Int

    = str!!.length >>> println(strLength("abc")) 3 >>> println(strLength(null)) // NullPointerException /VMMඇڐ༰ܕ
  80. Nullඇڐ༰ܕͷѻ͍ํ • ඇNullද໌(!!)͸ۃྗ࢖ΘͣʹɺԼͷ3ͭΛ৔໘ʹԠͯ͡࢖͍෼͚Δ • ifจʹΑΔnullνΣοΫ • ҆શݺͼग़͠ (?.) • ΤϧϏεԋࢉࢠ(?:)

    • ҆શݺͼग़͠ʹείʔϓؔ਺Λ૊Έ߹Θͤͯ࢖͏ํ๏΋͋Δ
  81. είʔϓؔ਺ͱNullڐ༰ܕ είʔϓؔ਺ʹ͸ let / apply / with / run /

    also ͕͋Δ Nullඇڐ༰ܕΛҾ਺ʹͱΔؔ਺Λݺͼग़࣌͢ʹศར ϥϜμ(ޙड़)ͱ૊Έ߹Θͤͯ࢖͏ fun sendEmailTo(email: String) { /*...*/ } val email: String? = ... sendEmailTo(email) //Error! email?.let { sendEmailTo(it) } //OK! /VMMඇڐ༰ܕ
  82. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  83. Lambda(ϥϜμ) ஋ͱͯ͠ѻ͑Δؔ਺ ߴ֊ؔ਺ͷҾ਺ʹͳΓಘΔ persons.maxBy { it.age } fun <T, R

    : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R): T? Ҿ਺͕ؔ਺ܕ
  84. LambdaͷҾ਺ͷলུ ϥϜμͷҾ਺Λলུͤͣʹॻ͘͜ͱ΋ग़དྷΔ it͸ϥϜμͷҾ਺͕1ͭͰɺҾ਺ͷఆٛΛলུͨ͠৔߹ʹͷΈ࢖͑Δ persons.maxBy { it.age } persons.maxBy { p:Person

    -> p.age }
  85. LambdaΛҾ਺ʹऔΔ৔߹ͷ()ͷলུ ϥϜμ͕Ҿ਺ͷ࠷ޙʹ͋Δ৔߹ɺͦΕΛ()ͷ֎ʹग़͢͜ͱ͕ग़དྷΔ ϥϜμ͕།ҰͷҾ਺ͷ৔߹ɺ()Λলུ͢Δ͜ͱ͕ग़དྷΔ persons.maxBy { it.age } persons.maxBy(){ it.age }

    persons.maxBy({ it.age }) persons.maxBy(){ p:Person -> p.age } persons.maxBy({ p:Person -> p.age })
  86. SAMม׵ ϥϜμ͸Javaͷಗ໊Ϋϥεͷ୅ΘΓʹ΋࢖͏͜ͱ͕ग़དྷΔ ͨͩͦ͠ͷΠϯλϑΣʔε͕1͔ͭ͠ந৅ϝιουΛ࣋ͨͳ͍৔߹ʹݶΔ SAMม׵(Single Abstract Method)ͱݺ͹ΕΔ //Java button.setOnClickListener(new View.OnClickListener() {

    @Override public void onClick(View view) { /* actions on click */ } }); //Kotlin button.setOnClickListener { /* actions on click */ }
  87. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  88. Kotlinͷඪ४ϥΠϒϥϦ είʔϓؔ਺΍ίϨΫγϣϯૢ࡞(maxBy΋ͦͷ1ͭ)ͳͲͷศརͳؔ਺܈͕ ఏڙ͞Ε͍ͯΔ αΠζ͕খ͍͞ͷͰAndroidͷapkʹؚΊͯ΋໰୊ͳ͍ ଟ͕֦͘ுؔ਺ fun <T, R : Comparable<R>>

    Iterable<T>.maxBy(selector: (T) -> R): T?
  89. ֦ுؔ਺ Ϋϥε֎͔ΒΫϥεʹؔ਺Λ௥Ճ͢Δ͜ͱ͕ग़དྷΔ ֦ுͨ͠ΫϥεΛϨγʔόܕͱݺͿ fun String.toInt(): Int = java.lang.Integer.parseInt(this) val i

    = "120".toInt() ֦ுର৅ͷΫϥε UIJT͸֦ுͨ͠Ϋϥε ͷΦϒδΣΫτ
  90. ঺հͨ͠Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  91. Kotlin ͷݴޠػೳ • Kotlinͷݴޠػೳ͸ɺଞʹ΋ͨ͘͞Μ͋Δ • Kotlin͸࣮༻ओٛͰɺ؆ܿͰɺ҆શͰɺJavaͱͷ૬ޓӡ༻ੑΛॏࢹͨ͠ ݴޠ • ·ͣ͸ɺ҆શੑ(Null҆શ)ʹҙࣝ͠ͳ͕Βॻ͖࢝Ίͯɺঃʑʹ༷ʑͳػ ೳΛ࢖͍࢝ΊΔͱྑ͍

  92. Kotlinͷπʔϧ

  93. Kotlin Λ࢝ΊΔͨΊͷπʔϧ • Android Studio • Convert Java to Kotlin

  94. Android Studio • 3.0Ҏ߱ͰKotlinͷPlugin͕σϑΥϧτͰೖͬͨ • ͦΕҎલͷόʔδϣϯͰ΋PluginΛೖΕΔ͜ͱͰKotlinͰͷ։ൃ͕Մೳ

  95. Android Studio 3.0 ϓϩδΣΫτ։࢝࣌ʹKotlinͰ࢝ΊΔ͜ͱ͕ग़དྷΔ

  96. buid.gradle buildscript { ext.kotlin_version = '1.1.3-2' dependencies { classpath 'com.android.tools.build:gradle:3.0.0-beta3'

    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } app/build.gradle apply plugin: ‘kotlin-android' dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" }
  97. Kotlin with Android Studio • Ͳͷબ୒ࢶͰ΋Մೳ • ϓϩδΣΫτ։࢝࣌ʹKotlin supportΛબͿ •

    Tools>Kotlin>Configure Kotlin in ProjectΛ࢖ͬͯKotlinϓϩδΣΫτԽ • खॻ͖ͰKotlinͷઃఆΛgradleʹ௥Ճ
  98. Convert Java to Kotlin • JavaͷϑΝΠϧΛબ୒ͯ͠ʮCodeʯϝχϡʔ͔ ΒʮConvert Java File to

    Kotlin FileʯΛબͿ͜ͱ Ͱίʔυ͕Kotlinʹม׵͞ΕΔ • JavaͷίʔυΛKotlinͷϑΝΠϧ(.kt)΁ίϐϖ͢ Δ͜ͱͰ΋Kotlinʹม׵͞ΕΔ
  99. Convert Java to Kotlin • Kotlinͷֶशʹศར • ͨͩ͠ɺྑ͍ίʔυͱ͸ݶΒͳ͍ͷͰཁ஫ҙ • ಛʹnull҆શʹ͸ؾΛ഑Δ

  100. ຊ೔ͷGoal • ʮKotlin ͱ͸Կͩʁʯˠ Kotlinͷߟ͑ํʹ͍ͭͯ஌Δ • ʮͳΜͰ Kotlinͳͷ͔ʁʯˠ KotlinͷݴޠػೳΛ஌Δ •

    ʮͲ͏΍ֶͬͯ΂͸ྑ͍ͷ͔ʁʯˠ KotlinΛ࢝ΊΔͨΊͷπʔϧΛ஌Δ
  101. ·ͱΊ • Kotlin͸࣮༻ओٛͰɺ؆ܿͰ҆શͳɺJavaͱͷ૬ޓӡ༻ੑ͕ߴ͍ݴޠ • ্هΏ͑ʹॻ͖ํͷࣗ༝౓͕ߴ͍ݴޠ • ༷ʑͳػೳ͕༻ҙ͞Ε͍ͯΔͷ΋Kotlinͷಛ௃ • ·ͣ͸Null҆શΛҙࣝ͠ͳ͕Βॻ͖࢝ΊΔ •

    ಋೖ͢Δ৔߹͸νʔϜ಺Ͱͷٞ࿦͸ඞਢ(ֶशظؒઃ͚Δͷ΋ྑ͍)
  102. How to start Kotlin • Official Site : kotl.in/ ,

    kotl.in/android • Kotlin is awesome! (kotlin.link/) • Kotlin in Action • Kotlin ελʔτϒοΫ(੺΂͜ຊ) • Kotlin Advent Calendar (2015/2016) • Kotlinೖ໳·Ͱͷॿ૸ಡຊ( goo.gl/5vUT7o )
  103. Thank you ! Let’s enjoy Kotlin