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.5k
What are AtCoder and competitive programming
kirimin
0
9.9k
バーチャル男声幼女プログラマーとして活動した1年間の振り返り
kirimin
0
1k
アプリエンジニアでも神絵師になりたい!
kirimin
4
5.3k
Watashi ni Kotlin ga maiorita
kirimin
0
490
NEMのAPIとモザイクであそぼう
kirimin
0
360
はじめようきれいなコード
kirimin
8
3k
Material Components for Android触ってみる
kirimin
7
1.9k
[社内LT]あたらしいMaterial Design
kirimin
1
1.7k
Other Decks in Programming
See All in Programming
Criando Commits Incríveis no Git
marcelgsantos
2
170
The rollercoaster of releasing an Android, iOS, and macOS app with Kotlin Multiplatform | droidcon Italy
prof18
0
150
Refactor your code - refactor yourself
xosofox
1
260
急成長期の品質とスピードを両立するフロントエンド技術基盤
soarteclab
0
920
return文におけるstd::moveについて
onihusube
1
860
CSC509 Lecture 14
javiergs
PRO
0
130
快速入門可觀測性
blueswen
0
330
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
330
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
210
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
750
Security_for_introducing_eBPF
kentatada
0
110
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Navigating Team Friction
lara
183
15k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
440
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
17
2.3k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Thoughts on Productivity
jonyablonski
67
4.4k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
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ʯ͕Φεεϝ ࠓճͨ͠Α͏ͳ༰͕ͱ͔ͯΓ͘͢ղ આ͞Ε͍ͯ·͢ʂ