Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kotlin Types and compatibility with Java
Search
きりみん
November 21, 2017
Programming
2
2k
Kotlin Types and compatibility with Java
きりみん
November 21, 2017
Tweet
Share
More Decks by きりみん
See All by きりみん
AndroidエンジニアがRailsにチャレンジしてる理由
kirimin
1
1.6k
What are AtCoder and competitive programming
kirimin
0
10k
バーチャル男声幼女プログラマーとして活動した1年間の振り返り
kirimin
0
1.1k
アプリエンジニアでも神絵師になりたい!
kirimin
4
5.5k
Watashi ni Kotlin ga maiorita
kirimin
0
560
NEMのAPIとモザイクであそぼう
kirimin
0
390
はじめようきれいなコード
kirimin
8
3.1k
Material Components for Android触ってみる
kirimin
7
2k
[社内LT]あたらしいMaterial Design
kirimin
1
1.7k
Other Decks in Programming
See All in Programming
パスタの技術
yusukebe
1
400
The state patternの実践 個人開発で培ったpractice集
miyanokomiya
0
150
Portapad紹介プレゼンテーション
gotoumakakeru
1
130
CSC305 Summer Lecture 12
javiergs
PRO
0
130
MLH State of the League: 2026 Season
theycallmeswift
0
160
AIレビュアーをスケールさせるには / Scaling AI Reviewers
technuma
2
230
Azure SRE Agentで運用は楽になるのか?
kkamegawa
0
150
技術的負債で信頼性が限界だったWordPress運用をShifterで完全復活させた話
rvirus0817
1
2.2k
エンジニアのための”最低限いい感じ”デザイン入門
shunshobon
0
130
あのころの iPod を どうにか再生させたい
orumin
2
2.5k
サーバーサイドのビルド時間87倍高速化
plaidtech
PRO
0
520
Oracle Database Technology Night 92 Database Connection control FAN-AC
oracle4engineer
PRO
1
220
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.9k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
820
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
30
9.6k
Six Lessons from altMBA
skipperchong
28
4k
How GitHub (no longer) Works
holman
315
140k
Build The Right Thing And Hit Your Dates
maggiecrowley
37
2.8k
Embracing the Ebb and Flow
colly
87
4.8k
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.9k
The Cost Of JavaScript in 2023
addyosmani
53
8.8k
Done Done
chrislema
185
16k
Transcript
SwiftΤϯδχΞʹհ͢Δ KotlinͷܕͱJavaͱͷޓੑ Swift/Kotlin߹ಉษڧձ #2
ࣗݾհ
ΘΓ͔ΜΛݱۚͰਫ਼ࢉ͠ͳͯ͘ޙ͔ΒεϚϗ্Ͱٻͨ͠Γग़དྷΔ ΞϓϦͩΑʂΘΓ͔Μ(ۉׂ)͡Όͳͯ͑͘ΔΑʂϥϯνͱ͔ʂ Android൛΄΅KotlinͰ࣮͞Ε͍ͯΔΑʂ
Kotlinʹ͍͓ͭͯ͞Β͍
Swiftͱಉ͘͡Nullڐ༰ܕͱ ඇڐ༰ܕ͕͋Δ • ѻ͍ํ͍͍ͩͨಉ͡ var str1: String = “hallo” str1
= null // ίϯύΠϧΤϥʔ var str2: String? = “konichiwa” str2 = null str2.length // ίϯύΠϧΤϥʔ str2?.length
Swiftͱಉ͘͡جຊతʹ ΠϛϡʔλϒϧͳΛ༻͢Δ • ѻ͍ํ͍͍ͩͨಉ͡ var str1 = “konichiwa” str1 =
“hallo” val str2 = “konichiwa” str2 = “hallo” // ίϯύΠϧΤϥʔ
ϓϦϛςΟϒܕͱࢀরܕ (ΦϒδΣΫτ)Λ۠ผ͠ͳ͍ • JavaʹϓϦϛςΟϒܕͱΦϒδΣΫτͱͯ͠ѻ ͏ͨΊͷϥούʔΫϥε͕͕͋ͬͨɺKotlinͰ۠ ผ͠ͳ͍ • int(Integer) → Int
• long(Long) → Long • boolean(Boolean) → Boolean
ίϨΫγϣϯෆมͱՄมʹ ͔Ε͍ͯΔ • σϑΥϧτͷίϨΫγϣϯෆม • ૢ࡞͍ͨ͠߹MutableListͳͲΛ͏ val list = listOf(“a”,
“b”, “c”) list.add(“d”) // ίϯύΠϧΤϥʔʂ(add()remove()ϝιου͑ͳ͍) val mList = mutableListOf(“a”, “b”, “c”) mList.add(“d”)
KotlinͷίϨΫγϣϯ֓ཁ $PMMFDUJPO .VUBCMF$PMMFDUJPO -JTU .VUBCMF-JTU
ͱ͜ΖͰɺKotlinJavaͱ શʹޓੑ͕͋Γ·͢
None
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; }
Kotlin͔ΒݟͨJavaม • JavaͰఆٛ͞ΕͨΛKotlin͔Βࢀর͢Δͱϓ ϥοτϑΥʔϜܕͱ͍͏ಛघͳܕʹͳΔ public String getStr(String str) { return
str; } ↓ fun getStr(str: String!): String! { return str }
ϓϥοτϑΥʔϜܕʁ • String!ͷΑ͏ʹදݱ͞ΕΔܕ • Nullڐ༰ܕ͔ඇڐ༰ܕ͔ෆ໌ͳঢ়ଶ • Ξϯϥοϓ͠ͳͯͦ͘ͷ··ࢀরͰ͖Δ • ࣮ͨͩ͠ߦ࣌ʹNullPointerException͕ൃੜ͠͏Δ •
JavaଆͰ@Nullable@NotNull͕͍͍ͯΕਖ਼͍͠ܕʹม͞ΕΔ • ͪͳΈʹintͳͲͷϓϦϛςΟϒܕnullʹͳΓ͑ͳ͍ͷͰৗʹඇڐ༰ ܕͱͯ͠ѻ͑Δ
ͳͥϓϥοτϑΥʔϜܕʁ • ͱͱJavaͰఆٛ͞Εͨͯ͢Nullableܕ ͱͯ͠ѻ͏Ҋ͋ͬͨ • ͔ͦ͠͠ΕͰJavaͷϥΠϒϥϦΛࢀরͨ͠߹ͳ Ͳʹίʔυ͕?ͩΒ͚ʹͳͬͯ͠·͍ݱ࣮తͰͳ͍ • ݱ࣮తͳརศੑΛॏࢹ͠ɺϓϥοτϑΥʔϜܕΛɹ ఆٛͨ͠
Java͔ΒݟͨKotlinίϨΫγϣϯ • KotlinͷListMutableListJava͔Β 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; }
Java͔ΒݟͨKotlinίϨΫγϣϯ • JavaͷίϨΫγϣϯͱKotlinͷίϨΫγϣϯ ޓੑ͕͋Γಛʹҙࣝ͢Δ͜ͱͳ͘ѻ͏ࣄ͕ Ͱ͖Δ • ͨͩ͠KotlinͰෆมͰએݴ͞Ε͍ͯΔίϨΫ γϣϯͰJava͔ΒมߋͰ͖ͯ͠·͏
Kotlin͔ΒݟͨJavaίϨΫγϣϯ • JavaͷListKotlin͔ΒMutableͳListͱͯ͠ ͦͷ··ѻ͑Δ • ListΛฦ͢JavaͷϝιουΛoverride͢Δ߹ ͳͲMutableͳListͱͯ͠Immutableͳ Listͱͯ͠ఆٛग़དྷΔ
KotlinͷίϨΫγϣϯ֓ཁ $PMMFDUJPO .VUBCMF$PMMFDUJPO -JTU .VUBCMF-JTU "SSBZ-JTU
Java͔ΒݟͨIntܕ • جຊతʹintʹม͞ΕΔ • ͨͩ͠ɺInt?ܕArray<Int>ܕ(ྻ)ͳͲΦ ϒδΣΫτͱͯ͠ѻ͏ඞཁ͕͋ΔͨΊɺ Integerܕͱͯ͠ѻΘΕΔ
Kotlin͔ΒݟͨϦςϥϧ • ٯʹKotlin͔ΒݟͨJavaͷϦςϥϧIntͳͲ ͷKotlinͷΦϒδΣΫτͱͯ͠ѻΘΕΔ
·ͱΊ • KotlinJavaͷΫϥεͱҧײͳ͘ڞଘͰ͖Δ ࣄΛڧ͘ҙࣝͨ͠ݴޠ༷ʹͳ͍ͬͯΔ • ݱ࣮తͳ։ൃݱͰͷརศੑΛॏࢹͨ͠ࣄ͕ Kotlin͕ਓؾݴޠʹͳͬͨཧ༝ͷҰ͔ͭ͠Ε ͳ͍
Kotlinͷ༷ΛͬͱΓͨ͘ͳͬ ͨΒʮKotlin in Actionʯ͕Φεεϝ ࠓճͨ͠Α͏ͳ༰͕ͱ͔ͯΓ͘͢ղ આ͞Ε͍ͯ·͢ʂ