Slide 1

Slide 1 text

͸ͯͳϒοΫϚʔΫ AndroidΞϓϦͰͷ Kotlinಋೖ id:takuji31 @Kan Java party 2017

Slide 2

Slide 2 text

஫ҙࣄ߲ • AndroidΞϓϦ։ൃͷલఏ஌ࣝͱͳΔΑ͏ͳ ෦෼ͷղઆ͸͠·ͤΜ • ͜ͷεϥΠυʹॻ͔Ε͍ͯΔKotlinͷίʔυ΍ ࢓༷͸όʔδϣϯ1.1.2࣌఺ͷ΋ͷͰ͢

Slide 3

Slide 3 text

ࣗݾ঺հ • ੢ྛ ୓ࢤ (ʹ͠͹΍͠ ͨ͘͡) • (hatena|twitter|github) takuji31 • גࣜձࣾ͸ͯͳ ΞϓϦέʔγϣϯΤϯδχΞ • ͸ͯͳϒοΫϚʔΫΞϓϦνʔϜॴଐ

Slide 4

Slide 4 text

ࣗݾ঺հ • Android։ൃ 2010೥2݄ʙ • Kotlin 2014೥12݄ʙ • iOS΍Webͷ։ൃ΋ᅂΉ

Slide 5

Slide 5 text

׆ಈ • Koreference • KotlinͰSharedPreferencesͷϞσϧΛ࡞Δ ϥΠϒϥϦʔ • Kansai.kt ӡӦ

Slide 6

Slide 6 text

ϓϥΠϕʔτ • Χϝϥপॅਓ • ϚΠϘ΢ϥʔ (AVG175) • ࠷ۙ͏͗͞Λࣂ͍࢝Ί·ͨ͠ • ΞΠίϯ͸͏ͪͷࢠͰ͢

Slide 7

Slide 7 text

ͱ͜ΖͰ

Slide 8

Slide 8 text

Έͳ͞ΜAndroidΞϓϦ ։ൃͯ͠·͔͢ʁ

Slide 9

Slide 9 text

Kotlinॻ͍ͯ·͔͢ʁ

Slide 10

Slide 10 text

ࠓ೔͸AndroidΞϓϦ։ൃʹ Ͳ͏΍ͬͯKotlinΛಋೖͯ͠ ͍͔͘Λ࿩͠·͢

Slide 11

Slide 11 text

AGENDA • Kotlinʹ͍ͭͯ • ͸ͯͳϒοΫϚʔΫʹ͍ͭͯ • ͸ͯͳϒοΫϚʔΫΞϓϦνʔϜͷߏ੒ • Kotlinಋೖ·ͰͷಓͷΓ • Kotlinͷಋೖͱͦͷޙ • KotlinԽͰى͖Δ໰୊ • KotlinͷݴޠػೳΛ׆༻͢Δ

Slide 12

Slide 12 text

Kotlin ʹ͍ͭͯ

Slide 13

Slide 13 text

JetBrainsࣾ (IntelliJ IDEAͷ։ൃݩ) ͕ ։ൃ͢Δݴޠ

Slide 14

Slide 14 text

Kotlinͷಛ௃ • Swift like ͳه๏ • Null safety • ܕਪ࿦ • ϓϩύςΟʔ • ϥϜμࣜ

Slide 15

Slide 15 text

Kotlinͷಛ௃ • ֦ுؔ਺ / ֦ுϓϩύςΟʔ • data class / sealed class • delegation • Coroutine (ex. async/await)

Slide 16

Slide 16 text

ଟ਺ͷϞμϯͳػೳΛ උ͍͑ͯΔ

Slide 17

Slide 17 text

Google I/O 2017Ͱ Android ΞϓϦͷެࣜ։ൃݴޠʹ

Slide 18

Slide 18 text

͸ͯͳϒοΫϚʔΫ ʹ͍ͭͯ

Slide 19

Slide 19 text

גࣜձࣾ͸ͯͳ͕ӡӦ͢Δ ιʔγϟϧϒοΫϚʔΫ αʔϏε

Slide 20

Slide 20 text

Web Android iOS ϒϥ΢βʔ֦ு

Slide 21

Slide 21 text

Web͸ScalaͰͷϦχϡʔΞ ϧ͕ਐߦத

Slide 22

Slide 22 text

AndroidΞϓϦ͸ઌ೔ Ϧϊϕʔγϣϯ (େن໛ϦϑΝΫλϦϯά) ͕׬ྃ㊗

Slide 23

Slide 23 text

http://developer.hatenastaff.com/entry/ 2017/05/26/192200

Slide 24

Slide 24 text

͸ͯͳϒοΫϚʔΫ ΞϓϦνʔϜ ʹ͍ͭͯ

Slide 25

Slide 25 text

ϝϯόʔߏ੒ • σΟϨΫλʔ • ϓϥϯφʔ • σβΠφʔ • Android / iOSΞϓϦ྆ํͷσβΠϯ୲౰

Slide 26

Slide 26 text

ϝϯόʔߏ੒ • ΤϯδχΞ3໊ • Swiftίϛολʔ • όϦόϦͷAndroidΞϓϦΤϯδχΞ(iOSษڧத) • takuji31 • AndroidΞϓϦͷϝΠϯ୲౰͸takuji31

Slide 27

Slide 27 text

ΤϯδχΞ͸શ෦΍Δ

Slide 28

Slide 28 text

શ෦ • AndroidΞϓϦ • iOSΞϓϦ • API • اը΍ΞΠσΟΞग़͠ɺνʔϜӡӦʹ΋खΛग़ ͢

Slide 29

Slide 29 text

ࣾ಺Ͱ࠷΋ΞΫςΟϒͳϞό ΠϧΞϓϦ։ൃνʔϜ

Slide 30

Slide 30 text

Kotlin ಋೖ·ͰͷಓͷΓ

Slide 31

Slide 31 text

͸ͯͳͰͷ৽ٕज़ (ݴޠ΍ϛυϧ΢ΣΞ) ಋೖ

Slide 32

Slide 32 text

ಋೖ·Ͱ • ΤϯδχΞ͕ىҊ • ٕज़ձͰಋೖͷੋඇ͕ٞ࿦͞ΕΔ • CTO͕ঝೝ

Slide 33

Slide 33 text

Kotlinͷ৔߹ • I/O Keynote͕ऴΘͬͨ௚ޙͷேɺKotlinಋೖ ىҊ (5/18) • ٕज़ձ • ঝೝ (5/19) • ͜͜·Ͱ͓Αͦ1೔൒

Slide 34

Slide 34 text

Kotlin+͸ͯϒͷ৔߹ • (ि໌͚ͯ) 22೔͸νʔϜͷD͕ෆࡏͩͬͨͷ Ͱ1ճٳΈ • 23೔ேʹDͱձ࿩ͯ͠ಋೖܾఆ • ଈ೔ಋೖ

Slide 35

Slide 35 text

Kotlin ಋೖͱͦͷޙ

Slide 36

Slide 36 text

͍Α͍ΑKotlinಋೖ

Slide 37

Slide 37 text

खॱ • طଘͷΞϓϦέʔγϣϯΛͲ͏KotlinԽ͍ͯ͠ ͔͘ߟ͑Δ • ಋೖ • KotlinԽ

Slide 38

Slide 38 text

طଘͷΞϓϦέʔγϣϯΛͲ ͏KotlinԽ͍͔ͯ͘͠

Slide 39

Slide 39 text

Ξϓϩʔν • શ෦Ұؾʹม׵͢Δ • ৽نίʔυ͚ͩKotlinͰॻ͘ • ৽نίʔυΛKotlinͰॻ͖ͭͭɺؔ࿈͢Δ෦෼ Λม׵͢Δ

Slide 40

Slide 40 text

શ෦Ұؾʹஔ͖׵͑Δ

Slide 41

Slide 41 text

શ෦Ұؾʹஔ͖׵͑Δ • ·ͱΊͯKotlinϓϥάΠϯͷࣗಈม׵Λߦ͏ • ৽نίʔυ͸΋ͪΖΜKotlin

Slide 42

Slide 42 text

ϝϦοτ • શ෦ஔ͖׵ΘͬͯεοΩϦ͢Δ • Javaͱͷ૬ޓӡ༻Λߟ͑Δඞཁ͕ͳ͘ͳΔ • ؾ͍͍࣋ͪ

Slide 43

Slide 43 text

σϝϦοτ • diff͕ഁ໓͢Δ • Ұؾʹม׵ࣗಈม׵ͷखͷಧ͔ͳ͍ͱ͜Ζ ΛखಈͰमਖ਼޻਺∞ • ஔ͖׵Θͬͨͷ͸จ๏͚ͩͰɺKotlinΒ͍͠ॻ ͖ํ͸Կ΋Ͱ͖͍ͯͳ͍

Slide 44

Slide 44 text

࿘ອͰ͢Ͷ

Slide 45

Slide 45 text

৽نίʔυ͚ͩKotlinͰॻ͘

Slide 46

Slide 46 text

৽نίʔυ͚ͩKotlinͰॻ͘ • ৽͍͠ϑΝΠϧ͚ͩKotlinʹ͢Δ • طଘͷJavaίʔυ͸ϊʔλον • طଘ෦෼ͷमਖ਼͸ͦͷ··JavaͰॻ͘

Slide 47

Slide 47 text

ϝϦοτ • ม׵ʹΑΔdiff͕ͳ͍ • KotlinͷֶशϖʔεΛίϯτϩʔϧ͠΍͍͢ • ͱΓ͋͑ͣ෼͔Βͳ͔ͬͨΒJavaͰॻ͍ͨΒ ͍͍ͱ͍͏͜ͱʹͰ͖Δ • ม׵޻਺θϩ

Slide 48

Slide 48 text

σϝϦοτ • ಋೖײബ͍ • Javaͱͷ૬ޓӡ༻Λҙࣝͨ͠ίʔυΛॻ͔ͳ͚Ε͹ͳΒͳ͍ • JavaͱKotlinͰ಄ͷίϯςΩετεΠον͕ൃੜ͢ΔͷͰ͋·Γੜ ࢈ੑ͸ग़ͳ͍ʢݸਓͷײ૝Ͱ͢ʣ • ݁ہJava͹͔ͬΓॻ͔ΕͨΓ͢Δ͔΋͠Εͳ͍ • Ϟμϯͳػೳ͕࢖͍ͮΒ͔ͬͨΓ͢Δʢ֦ுؔ਺ͱ͔ʣ • ؾ࣋ͪΑ͘ͳ͍

Slide 49

Slide 49 text

҆શࡦͬͯ ײ͡Ͱ͢Ͷ

Slide 50

Slide 50 text

৽نίʔυΛKotlinͰॻ͖ͭ ͭɺؔ࿈͢Δ෦෼Λม׵͢Δ

Slide 51

Slide 51 text

৽نKotlinʴม׵ • ৽نίʔυ͸KotlinͰॻ͘ • ͦͷίʔυΛݺͼग़͢ઌ΍ɺͦ͜Ͱ࢖͏ίʔ υΛKotlinʹม׵͢Δ • طଘͷΫϥεʹϝιουΛ௥Ճ͢ΔΑ͏ͳ࣌ ʹ΋Kotlinʹม׵͢Δ

Slide 52

Slide 52 text

ϝϦοτ • ൺֱతߴ͍ಋೖײ • ͻͨ͢ΒKotlinΛॻ͚Δ • Javaͱͷ૬ޓӡ༻Λ΄ͱΜͲҙࣝ͠ͳͯ͘Α ͘ͳΔ • ؾ͍͍࣋ͪ

Slide 53

Slide 53 text

σϝϦοτ • diff͸·͊·͍͊͢͜͝ͱʹͳΔ • ͋ͳͨͱKotlinࠓֶ͙͢शɺͱ͍͏ײ͡ʹͳΔ • ม׵ίετ͸͋Δ • Ͳ͜·Ͱม׵͢Δ͔ͷઢҾ͖͕೉͍͠

Slide 54

Slide 54 text

όϥϯε͕ ྑͦ͞͏Ͱ͢Ͷ

Slide 55

Slide 55 text

Ξϓϩʔν • શ෦Ұؾʹม׵͢Δ • ৽نίʔυ͚ͩKotlinͰॻ͘ • ৽نίʔυΛKotlinͰॻ͖ͭͭɺؔ࿈͢Δ෦෼ ΛKotlinʹม׵ ࠾༻

Slide 56

Slide 56 text

ಋೖ

Slide 57

Slide 57 text

Gradleͷઃఆ Λ௥Ճ

Slide 58

Slide 58 text

Tools Kotlin Configure Kotlin in Project

Slide 59

Slide 59 text

͋ͱ͸ Android Studioʹ೚ͤͯ ͓͚͹ऴΘΔ

Slide 60

Slide 60 text

aptΛkaptʹ ஔ͖׵͑Δ

Slide 61

Slide 61 text

Slide 62

Slide 62 text

Kotlin޲͚ͷ ϥΠϒϥϦʔಋೖ

Slide 63

Slide 63 text

ϥΠϒϥϦʔΛಋೖ͢Δ͔ • KotlinͷݴޠػೳΛ࠷େݶ׆༻͢ΔͨΊʹ͸ඞ ཁ • KotlinΦϯϦʔͷϥΠϒϥϦʔ͸ΞΫςΟϒͳ ΋ͷ͕গͳ͍ͷͰ͸ʁ • KotlinͷݴޠόʔδϣϯͷมߋʹϥΠϒϥϦʔ ͕͍͍͚ͭͯΔ͔ʁ

Slide 64

Slide 64 text

ಋೖͷࢦ਑ • طଘϥΠϒϥϦʔͷ֦ு͸ੵۃతʹಋೖͯ͠ ͍͘ • KotlinΦϯϦʔͷϥΠϒϥϦʔ͸৻ॏʹબͿ

Slide 65

Slide 65 text

KotlinԽʹ౰ͨͬͯಋೖͨ͠ϥΠϒ ϥϦʔୈ1஄ • RxKotlin • RxJavaͷKotlin֦ு • ศརͳ֦ுؔ਺܈

Slide 66

Slide 66 text

KotlinԽʹ౰ͨͬͯಋೖͨ͠ϥΠϒ ϥϦʔୈ1஄ • PaperParcel • Java੡ͷParcelableࣗಈੜ੒ϥΠϒϥϦʔ • ͜Ε·Ͱ͸IDEͷࣗಈੜ੒ͰͳΜͱ͔͍ͯͨ͠ • ͔͔ͤͬͩ͘Βboilerplate codeݮΒ͍ͨ͠ͷͰಋೖ • Kotlinͷdata classαϙʔτ • ଞͷParcelableܥϥΠϒϥϦʔΑΓKotlinͱͷ૬ੑ͕Α͔ͬͨ

Slide 67

Slide 67 text

KotlinԽʹ౰ͨͬͯಋೖͨ͠ϥΠϒ ϥϦʔୈ1஄ • Koreference • ੿࡞ • SharedPreferencesΛ͍͍ײ͡ʹModelͬΆ͘ѻ͏ϥΠϒϥϦʔ • ϓϩύςΟʔΛఆٛ͢Δ͚ͩͰSharedPreferencesͷ஋Λૢ࡞Ͱ͖Δ • RxJava2ʹΑΔมߋ஋؂ࢹαϙʔτ • ͱʹ͔͘खͰSharedPreferencesͷॲཧॻ͖ͨ͘ͳ͍ͷͰಋೖ • υοάϑʔσΟϯά΋݉Ͷͯ

Slide 68

Slide 68 text

ϥΠηϯεදه

Slide 69

Slide 69 text

Kotlin͸Apache2ϥΠηϯ ε

Slide 70

Slide 70 text

ඪ४ϥΠϒϥϦʔ͕ґଘͰ ೖΔͷͰϥΠηϯε΋දه ͠·͠ΐ͏

Slide 71

Slide 71 text

Slide 72

Slide 72 text

Slide 73

Slide 73 text

KotlinԽ

Slide 74

Slide 74 text

·ͣ͸ࢼͯ͠Έͨ

Slide 75

Slide 75 text

৽͍͠ActivityΛKotlinͰ

Slide 76

Slide 76 text

৽͍͠ActivityΛKotlinͰ • ৽نϑΝΠϧϝχϡʔͷKotlin ActivityΛબ୒ • Ұ୴ଞͷΫϥε͸ม׵ͤͣʹActivity͚ͩ KotlinͰॻ͘ • Pull Request -> merge

Slide 77

Slide 77 text

طଘͷRepositoryʹ ϝιουΛ௥Ճ͢Δ

Slide 78

Slide 78 text

طଘͷRepositoryʹϝιουΛ ௥Ճ͢Δ • ΫϥεϑΝΠϧΛCode Convert Java file to Kotlin fileͰKotlinʹ ม׵ • KotlinΒ͘͠ॻ͚Δͱ͜Ζ͸ॻ͖׵͑Δ • ֦ுؔ਺ • ϓϩύςΟʔ • ϥϜμࣜ • Repository͔Βฦ͢model͸Kotlinͷdata class

Slide 79

Slide 79 text

طଘͷRepositoryʹϝιουΛ ௥Ճ͢Δ • ϩδοΫͷ࣮૷ • Pull Request merge

Slide 80

Slide 80 text

Kotlin ಋೖͰى͖Δ໰୊

Slide 81

Slide 81 text

diffഁ໓໰୊

Slide 82

Slide 82 text

ΫϥεAΛJava Kotlin ͢Δ = A.java Λফͯ͠ A.ktΛ௥Ճ

Slide 83

Slide 83 text

2ഒͷdiff

Slide 84

Slide 84 text

ίʔυͷमਖ਼ΛೖΕͨΒɺ ߋʹͦͷத͔ΒมߋΛݟ͚ͭ ͳ͚Ε͹ͳΒͳ͍ɻ

Slide 85

Slide 85 text

Slide 86

Slide 86 text

ม׵͚ͩઌʹ΍͓ͬͯ͘ (Pull RequestΛ෼͚Δ)

Slide 87

Slide 87 text

gitྺ࢙ফ໓໰୊

Slide 88

Slide 88 text

ม׵͢Δ͜ͱͰผϑΝΠϧʹ ͳΔͷͰɺgitͷཤྺ͕௥͑ͳ ͘ͳΔ

Slide 89

Slide 89 text

ࣗಈม׵͕gitͰrenameѻ͍ ʹͳΕ͹ϥοΩʔ͕ͩɺͦ͏ ͍͏ϑΝΠϧ͸ن໛͕খ͍͞ ͷͰࠔΒͳ͍͜ͱ͕ଟ͍

Slide 90

Slide 90 text

࠷ѱͷղܾํ๏

Slide 91

Slide 91 text

.ktʹϦωʔϜͯ͠ίϛοτ .javaʹ໭ͯ͠ม׵ͯ͠ ίϛοτ

Slide 92

Slide 92 text

͜ͷํ๏Ͱ git log --follow Ͱ௥͑ΔΑ͏ʹͳΔ

Slide 93

Slide 93 text

(࢖ͬͯͳ͍)

Slide 94

Slide 94 text

ϨϏϡʔίετ ߴ͗͢໰୊

Slide 95

Slide 95 text

ίʔυϨϏϡϫʔ͕Kotlinʹ ׳Εͯͳ͗ͯ͢͞ϨϏϡʔਫ਼ ౓͕Լ͕ͬͨΓɺεϐʔυ͕ མͪΔ

Slide 96

Slide 96 text

׳Εͷ໰୊

Slide 97

Slide 97 text

ϒοΫϚʔΫΞϓϦνʔϜ Ͱ͸ϖΞͰίʔυϨϏϡʔ

Slide 98

Slide 98 text

ձ࿩Ͱٙ໰Λղܾ

Slide 99

Slide 99 text

Slide 100

Slide 100 text

Kotlinॻ͚ͳ͍໰୊

Slide 101

Slide 101 text

͍͖ͳΓ৽͍͠ݴޠॻ͚ͳ͍ (౰ͨΓલ)

Slide 102

Slide 102 text

ॻ͚Δਓ͕νʔϜʹ1,2ਓ͔͠ ͍ͳ͍…ʁ

Slide 103

Slide 103 text

ಛʹJavaҎ֎ॻ͍ͨ͜ͱ͕ ͳ͍ɺͱ͍͏ਓ

Slide 104

Slide 104 text

≒ Ϟμϯͳݴޠͷॻ͖ํ͕෼ ͔Βͳ͍

Slide 105

Slide 105 text

ॻ͚Δਓͱ ϖΞϓϩɺϖΞϨϏϡʔ ͠·͠ΐ͏

Slide 106

Slide 106 text

ϒοΫϚʔΫΞϓϦνʔϜ͸ ͳΜͱͳ͘ॻ͚Δਓ͕ ἧ͍ͬͯͨ☺

Slide 107

Slide 107 text

୭΋ॻ͚Δਓ͕͍ͳ͍ͳΒ ·ͣಋೖલʹֶशͯ͘͠Εʂ

Slide 108

Slide 108 text

kotlinlang.org/docs/ reference/

Slide 109

Slide 109 text

try.kotlinlang.org

Slide 110

Slide 110 text

Kotlin ͷݴޠػೳΛ ׆༻͢Δ

Slide 111

Slide 111 text

Property

Slide 112

Slide 112 text

Πϯελϯεͷঢ়ଶΛ ද͢࢓૊Έ

Slide 113

Slide 113 text

getter (+setter) (+ backing field)

Slide 114

Slide 114 text

Property class User(id: Long, name: String) {
 val id: Long
 var name: String
 var registered: Boolean = false
 
 init {
 this.id = id
 this.name = name
 }
 
 }


Slide 115

Slide 115 text

Property class User(id: Long, name: String) {
 val id: Long
 var name: String
 var registered: Boolean = false
 
 init {
 this.id = id
 this.name = name
 }
 
 }
 ಡΈऔΓઐ༻

Slide 116

Slide 116 text

Property class User(id: Long, name: String) {
 val id: Long
 var name: String
 var registered: Boolean = false
 
 init {
 this.id = id
 this.name = name
 }
 
 }
 ಡΈऔΓઐ༻ ಡΈॻ͖Մ

Slide 117

Slide 117 text

Property class User(id: Long, name: String) {
 val id: Long
 var name: String
 var registered: Boolean = false
 
 init {
 this.id = id
 this.name = name
 }
 
 }
 ಡΈऔΓઐ༻ ಡΈॻ͖Մ ܕ

Slide 118

Slide 118 text

Property class User(id: Long, name: String) {
 val id: Long
 var name: String
 var registered: Boolean = false
 
 init {
 this.id = id
 this.name = name
 }
 
 }
 ಡΈऔΓઐ༻ ಡΈॻ͖Մ ܕ ॳظ஋

Slide 119

Slide 119 text

Property class User(id: Long, name: String) {
 val id: Long
 var name: String
 var registered: Boolean = false
 
 init {
 this.id = id
 this.name = name
 }
 
 }
 ಡΈऔΓઐ༻ ಡΈॻ͖Մ ܕ ॳظ஋ JOJUϒϩοΫͰ ॳظԽ

Slide 120

Slide 120 text

͜͏ஔ͖׵͑ΒΕ·͢ class User(val id: Long, var name: String) {
 var registered: Boolean = false
 }


Slide 121

Slide 121 text

͜͏ஔ͖׵͑ΒΕ·͢ class User(val id: Long, var name: String) {
 var registered: Boolean = false
 }
 ίϯετϥΫλʔ಺Ͱ ϓϩύςΟʔఆٛ͢Δͱɺࣗಈతʹ୅ೖ͞ΕΔ

Slide 122

Slide 122 text

registered΋஋Λड͚औΕΔΑ͏ʹ class User(
 val id: Long,
 var name: String,
 var registered: Boolean = false
 )


Slide 123

Slide 123 text

registered΋஋Λड͚औΕΔΑ͏ʹ class User(
 val id: Long,
 var name: String,
 var registered: Boolean = false
 )
 SFHJTUFSFEΛ౉͞ͳ͔ͬͨΒGBMTF͕୅ೖ͞ΕΔ

Slide 124

Slide 124 text

data class

Slide 125

Slide 125 text

ԿΒ͔ͷσʔλʔΛอ࣋͢Δ ͨΊͷΫϥε

Slide 126

Slide 126 text

Model΍DTOΛ࡞ΔͨΊͷ ศརػೳຬࡌ

Slide 127

Slide 127 text

ex.) طଘͷmodelΛKotlinʹ ม׵͍ͨ͠

Slide 128

Slide 128 text

ม׵͸Convert Java file to Kotlin fileͰ

Slide 129

Slide 129 text

୯७ͳม׵͚ͩͰΑ͍ͷ͔ʁ

Slide 130

Slide 130 text

୯७ͳม׵͚ͩͰΑ͍ͷ͔ʁ • Immutableʹ͍ͨ͠ • equals/hashCode/toStringͷ࣮૷

Slide 131

Slide 131 text

Immutableʹ

Slide 132

Slide 132 text

શͯͷϓϩύςΟʔΛvalͰ ఆٛ + constructorͰ୅ೖ

Slide 133

Slide 133 text

એݴ class User(
 val id: Long,
 val name: String,
 val registered: Boolean
 )

Slide 134

Slide 134 text

࢖͏ fun main(args: Array) {
 val unregisteredUser = User( id = 114514L, name = "takuji31", registered = false )
 val user = register(unregisteredUser)
 print(user)
 }
 
 fun register(user: User): User {
 // do something
 return User(user.id, user.name, true)
 }

Slide 135

Slide 135 text

ίϐʔΛ΋ͬͱ͍͍ײ͡ʹ͠ ͍ͨ

Slide 136

Slide 136 text

data classΛ࢖͏

Slide 137

Slide 137 text

એݴ data class User(
 val id: Long,
 val name: String,
 val registered: Boolean = false
 )

Slide 138

Slide 138 text

એݴ data class User(
 val id: Long,
 val name: String,
 val registered: Boolean = false
 ) ಄ʹEBUB Λ͚ͭΔ

Slide 139

Slide 139 text

એݴ data class User(
 val id: Long,
 val name: String,
 val registered: Boolean = false
 ) ಄ʹEBUB Λ͚ͭΔ શͯͷϓϩύςΟʔΛ DPOTUSVDUPSͰఆٛ

Slide 140

Slide 140 text

࢖͏ fun register(user: User): User {
 // do something
 return user.copy(registered = true)
 }

Slide 141

Slide 141 text

࢖͏ fun register(user: User): User {
 // do something
 return user.copy(registered = true)
 } DPQZϝιουͷҾ਺ʹ มߋ͍ͨ͠஋͚ͩΛ౉͢ͱ ίϐʔ͞ΕΔ

Slide 142

Slide 142 text

equals/hashCode/toString ͷ࣮૷

Slide 143

Slide 143 text

data classΛ࢖͏ͱࣗಈੜ੒ ͞ΕΔ

Slide 144

Slide 144 text

͸ͯͳϒοΫϚʔΫAndroid ΞϓϦͰ͸ modelΛdata class΁ ੵۃతʹஔ͖׵͍͑ͯ·͢

Slide 145

Slide 145 text

Extension

Slide 146

Slide 146 text

֦ுؔ਺/֦ுϓϩύςΟʔ

Slide 147

Slide 147 text

طଘͷΫϥεʹ ؔ਺΍ϓϩύςΟʔΛੜ΍͢ (Α͏ʹݟͤΔ)࢓૊Έ

Slide 148

Slide 148 text

ϑϨʔϜϫʔΫ΍ϥΠϒϥϦʔ ͷΫϥεΛ֦ு͍ͯ͘͠ͱΑ ͍

Slide 149

Slide 149 text

FragmentΛ֦ுϓϩύςΟʔͰ ֦ு͢Δ val Fragment.baseActivity: BaseActivity?
 get() = activity as BaseActivity?
 val Fragment.actionBar: ActionBar?
 get() = baseActivity?.supportActionBar

Slide 150

Slide 150 text

FragmentΛ֦ுϓϩύςΟʔͰ ֦ு͢Δ val Fragment.baseActivity: BaseActivity?
 get() = activity as BaseActivity?
 val Fragment.actionBar: ActionBar?
 get() = baseActivity?.supportActionBar ֦ு͍ͨ͠ܕ Λॻ͘

Slide 151

Slide 151 text

FragmentΛ֦ுϓϩύςΟʔͰ ֦ு͢Δ val Fragment.baseActivity: BaseActivity?
 get() = activity as BaseActivity?
 val Fragment.actionBar: ActionBar?
 get() = baseActivity?.supportActionBar ֦ு͍ͨ͠ܕ Λॻ͘ ͦͷଞ͸௨ৗͷ ϓϩύςΟʔͱಉ͡

Slide 152

Slide 152 text

RealmͷProviderΛ֦ுؔ਺Ͱ ֦ு͢Δ fun Provider.getAutoCloseObservable(): Observable {
 val realm = get()
 realm.use {
 return Observable.create { emitter ->
 val instance = get()
 emitter.setCancellable {
 instance.close()
 }
 emitter.onNext(instance)
 }
 }
 }

Slide 153

Slide 153 text

RealmͷProviderΛ֦ுؔ਺Ͱ ֦ு͢Δ fun Provider.getAutoCloseObservable(): Observable {
 val realm = get()
 realm.use {
 return Observable.create { emitter ->
 val instance = get()
 emitter.setCancellable {
 instance.close()
 }
 emitter.onNext(instance)
 }
 }
 } ֦ு͍ͨ͠ܕ Λॻ͘

Slide 154

Slide 154 text

RealmͷProviderΛ֦ுؔ਺Ͱ ֦ு͢Δ fun Provider.getAutoCloseObservable(): Observable {
 val realm = get()
 realm.use {
 return Observable.create { emitter ->
 val instance = get()
 emitter.setCancellable {
 instance.close()
 }
 emitter.onNext(instance)
 }
 }
 } ֦ு͍ͨ͠ܕ Λॻ͘ (FOFSJDTͷ ܕύϥϝʔλʔ΋ࢦఆՄ
 ϓϩύςΟʔ΋ಉ༷

Slide 155

Slide 155 text

RealmͷProviderΛ֦ுؔ਺Ͱ ֦ு͢Δ fun Provider.getAutoCloseObservable(): Observable {
 val realm = get()
 realm.use {
 return Observable.create { emitter ->
 val instance = get()
 emitter.setCancellable {
 instance.close()
 }
 emitter.onNext(instance)
 }
 }
 } ֦ு͍ͨ͠ܕ Λॻ͘ ͦͷଞ͸௨ৗͷ ؔ਺ͱಉ͡ (FOFSJDTͷ ܕύϥϝʔλʔ΋ࢦఆՄ
 ϓϩύςΟʔ΋ಉ༷

Slide 156

Slide 156 text

஫ҙ఺

Slide 157

Slide 157 text

࣮ࡍʹ֦ு͞ΕΔ ͷͰ͸ͳ͘ɺ ࣮ମ͸staticͳ ϝιουݺͼग़͠

Slide 158

Slide 158 text

= ֦ுϓϩύςΟʔ͸ϑΟʔ ϧυΛ࣋ͯͳ͍

Slide 159

Slide 159 text

ఘΊͯଞͷΞϓϩʔνΛ ߟ͑·͠ΐ͏

Slide 160

Slide 160 text

֦ு͗͢͠Δͱ Θ͚͕Θ͔Βͳ͘ͳΔ

Slide 161

Slide 161 text

΍Γա͗஫ҙ

Slide 162

Slide 162 text

Delegated property

Slide 163

Slide 163 text

ҕৡϓϩύςΟʔ

Slide 164

Slide 164 text

ॲཧΛҕৡ͢Δ ϓϩύςΟʔΛ ࡞Δ͜ͱ͕Ͱ͖Δ

Slide 165

Slide 165 text

σϑΥϧτͷ Delegated property(Ұ෦) • Delegates.notNull • not-null͕ͩॳظԽ࣌ʹॳظԽͰ͖ͳ͍ϓϩύςΟʔ • nullͷ··ΞΫηεͨ͠Βྫ֎ • lazy • ஗ԆॳظԽ • ࠷ॳͷΞΫηε࣌ʹҾ਺Ͱ౉ͨ͠ϒϩοΫͷ໭Γ஋Λฦͯ͠ Ωϟογϡ͢Δ

Slide 166

Slide 166 text

ଟ෼͕࣌ؒ଍Γͳ͍ͷͰ

Slide 167

Slide 167 text

see. github.com/takuji31/ Koreference

Slide 168

Slide 168 text

Enjoy Kotlin Life