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だって間違えるとnullでハマるよ!~
Search
RyotaMurohoshi
July 24, 2017
Technology
6
1.9k
プラットホーム型について ~Kotlinだって間違えるとnullでハマるよ!~
2017/07/24
『2017年7月定例会「Kotlin & Android Studio 3.0」』
の発表資料です
RyotaMurohoshi
July 24, 2017
Tweet
Share
More Decks by RyotaMurohoshi
See All by RyotaMurohoshi
Unityの合同同人誌や合同商業誌を書いてる僕は感想やレビューや評価が欲しい
ryotamurohoshi
0
420
Unity 2021.1での Unityパッケージの名称変更について
ryotamurohoshi
0
650
Odin Validationはいいぞ!
ryotamurohoshi
2
880
Tilemapはいいぞ!2020 〜すごいぞ、プロジェクト専用拡張Brush〜
ryotamurohoshi
0
2.3k
Unityでも、新しいC#
ryotamurohoshi
0
1.2k
Riderはいいぞ!
ryotamurohoshi
1
3.3k
Riderのススメ〜俺はRiderここが好き〜
ryotamurohoshi
1
2.3k
Unity開発者に伝えたい.NETのこと
ryotamurohoshi
4
36k
ImportedLinqのススメ
ryotamurohoshi
0
1.3k
Other Decks in Technology
See All in Technology
LINEスキマニにおけるフロントエンド開発
lycorptech_jp
PRO
0
340
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
320
2024年にチャレンジしたことを振り返るぞ
mitchan
0
150
[Ruby] Develop a Morse Code Learning Gem & Beep from Strings
oguressive
1
190
Google Cloud で始める Cloud Run 〜AWSとの比較と実例デモで解説〜
risatube
PRO
0
120
Yahoo! ズバトクにおけるフロントエンド開発
lycorptech_jp
PRO
0
100
JVM(JavaVM)の性能分析者観点で探るInstanaの可能性
instanautsjp
0
120
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
130
DevFest 2024 Incheon / Songdo - Compose UI 조합 심화
wisemuji
0
140
OCI技術資料 : ファイル・ストレージ 概要
ocise
3
11k
プロダクト開発を加速させるためのQA文化の築き方 / How to build QA culture to accelerate product development
mii3king
1
290
5分でわかるDuckDB
chanyou0311
10
3.3k
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
169
14k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
4 Signs Your Business is Dying
shpigford
182
21k
Learning to Love Humans: Emotional Interface Design
aarron
274
40k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
GitHub's CSS Performance
jonrohan
1031
460k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Side Projects
sachag
452
42k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Faster Mobile Websites
deanohume
305
30k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Transcript
ϓϥοτϗʔϜܕʹ͍ͭͯ ~Kotlinͩͬͯؒҧ͑ΔͱnullͰϋϚΔΑʂ~ @RyotaMurohoshi 2017/07/24ʮKotlin-&-Android-Studio-3.0ʯ
Kotlinͷ͍͍ͱ͜Ζͱ͍͑ ԿΛࢥ͍ු͔·͔͢ʁ
ίʔυ͕؆ܿʹॻ͚Δͱ͜Ζʁ
Javaͱͷੑʁ
nullΛ͍͍ײ͡ʹѻ͑Δͱ͜Ζʁ
KotlinͷެࣜυΩϡϝϯτʹ ͜Μͳϖʔδ͋Γ·͢Ͷ
Null$Safety h"ps:/ /kotlinlang.org/docs/reference/null6safety.html
ɹͱɾ͜ɾΖɾͰ
KotlinͩͬͨΒ NullPointerExcep/on ͕ى͖ͳ͍
ͬͯࢥ͍ͬͯ·ͤΜ͔ʁ
KotlinͰ NullPointerExcep/on ى͖·͢
KotlinͰ NullPointerExcep/on ى͖·͢ʂʂʂ
͚ΕͲKotlin͕ѱ͍Θ͚Ͱͳ͘ ͍ํ͕ؒҧ͍ͬͯΔͷͰ͢
• ࠷ۙKotlinΛ৮Γ࢝Ίͨ • Googleͷൃද͔ΒKotlinʹڵຯΛ࣋ͬͯ৮Γ࢝Ίͨ • ڈ͋ͨΓ͔ΒͳΜͱͳ͘Kotlinؾʹͳͬͯ৮Γ࢝Ίͨ ࠓ͜ͷΑ͏ͳํʹ͚ͯ
Android+KotlinΔ্Ͱ Ͳ͏͓͍͍͖͍ͯͬͯͯͨͩͨ͠ɺ
KotlinͱnullͱϓϥοτϗʔϜʹ͍ͭͯ ൃද͍͖ͤͯͨͩ͞·͢
͜Ε͔Β͓͢Δ͜ͱ Kotlinͷ͍͚ͯΔݴޠػೳͰ Android+Kotlinͷ͍͚ͯΔϊϋͰ͋Γ·ͤΜ
͚ΕͲɺ͜Ε͔Β͓͢Δ͜ͱΛཧղ͢Ε όάΛੜΜͰ͠·͏͜ͱΛආ͚ΒΕΔ͔͠Ε·ͤΜ
ࣗݾհ
• @RyotaMurohoshi • ΉΖ΄͠ • KotlinॿಡຊʹࢀՃ • ͔Θ͍͍Kotlinͱ͔Sansan༷ͷษڧձͰLT
Kotlinͱͷग़ձ͍ @ngsw_taro͞Μͷ͋ΔΠϕϯτͰͷ Kotlinͱnullʹؔ͢ΔLTͰͨ͠
Kotlinͱnull جຊͷ͓͞Β͍
StringܕʹnullೖͰ͖ͳ͍ // ίϯύΠϧΤϥʔ val str: String = null
String?ܕͳΒnullೖͰ͖Δ // OK val str: String? = null
String͘͠nullΛฦ͢readMessage ฦܕʮString?ʯܕʹ͢Δ fun readMessage(): String? { // จࣈྻϦςϥϧΛฦ͔͢ // nullΛฦ͔͢
// ུ }
String͕null͔͠Εͳ͍ ͱ͍͏ͱ͖String?Λ Θͳ͍ͱ͍͚ͳ͍
ͯ͞
StringܕʹString?ೖͰ͖ͳ͍ // ࣍ͷؔ // StringϦςϥϧΛฦ͔͢͠Εͳ͍͠ // nullΛฦ͔͢͠Εͳ͍ fun readMessage(): String?
{ // ུ } // ίϯύΠϧΤϥʔ val message: String = readMessage()
String?ܕʹStringೖͰ͖Δ // ࣍ͷؔ // StringΛฦ͢ fun findTag(): String { //
ུ } // ࣍OK val tag: String? = findTag()
nullʹ·ͭΘΔϥϯλΠϜΤϥʔΛ ආ͚ΔͨΊͷૉఢݴޠ༷
val message: String? = readMessage() // ࣍ίϯύΠϧΤϥʔ // String?ͰJavaΈ͍ͨͳॻ͖ํͰ͖ͳ͍ val
messageLength: Int = message.length // ࣍ίϯύΠϧΤϥʔʹͳΒͳ͍͕ // message͕nullͳΒϥϯλΠϜΤϥʔ val messageLength: Int = message!!.length // ࣍OK // messageLengthIntܕͰͳ͘Int?ܕ val messageLength: Int? = message?.length
String?ܕͰJavaΈ͍ͨͳ ϝϯόΞΫηεͰ͖ͳ͍ ݪଇɺʮ?.ʯΛ͏ ͘͠ϥϯλΠϜΤϥʔʹͳΔՄೳੑͷ͋Δʮ!!.ʯ
ͳΔ΄ͲNullPointerExcep/onൃੜ͠ͳͦ͏ • StringܕʹnullೖͰ͖ͳ͍ • null͔͠Εͳ͍ͱ͍͏࣌String? • StringͱString?ผ • String??.Ͱ
͚ΕͲɺ
KotlinͰ NullPointerExcep/on ى͖·͢ʂ
͔ͨ͠ʹ Kotlin͚ͩͰίʔυΛॻ͘ͷͳΒ ͦ͏ͦ͏NullPointerExcep/onൃੜ͠ͳ͍
ͦ͏ɺKotlin͚ͩͰॻ͘ͳΒ
օ͞Μ͕ࣗॻ͘ίʔυΛશͯ Kotlinʹ͢Δ͜ͱͰ͖ͦ͏Ͱ͢Ͷ
ͦ͏͍͑ɺࠓAndroidͷձͷΠϕϯτͰͨ͠Ͷ
ͱ͜ΖͰɺօ͞ΜAndroidΞϓϦ։ൃ
ϥΠϒϥϦ͍·͢ΑͶʁ ϑϨʔϜϫʔΫ͍·͢ΑͶʁ
Java
JavaʹString?ͱ͔ɺͳ͍
JavaͷίʔυΛݺͼग़͢Օॴ ཁҙʂ
͜ΜͳJavaͷίʔυΛ Kotlin͔Βݺͼग़ͨ͠ΒͲ͏ͳΔͩΖ͏ʁ // ͜ΕJavaͰ͢ public class Reader { public static
String readNullableStringJava() { return null; } }
Kotlin͔Βݺͼग़͢ͱɻɻɻ // ͜ΕίϯύΠϧΤϥʔʹͳΒͳ͍ val str = Reader.readNullableStringJava() // ͜ΕίϯύΠϧΤϥʔʹͳΒͳ͍ val
str : String? = Reader.readNullableStringJava() // ʂ͜ΕίϯύΠϧΤϥʔʹͳΒͳ͍ʂʂʂ val str : String = Reader.readNullableStringJava()
શ෦OK ίϯύΠϧΤϥʔʹͳΒͳ͍
Ͱͪΐͬͱ͍ͬͯͩ͘͞
ʲ࠶ܝʳ͜ΜͳJavaͷίʔυΛ Kotlin͔Βݺͼग़ͨ͠ΒͲ͏ͳΔͩΖ͏ʁ // ͜ΕJavaͰ͢ public class Reader { public static
String readNullableStringJava() { return null; } }
ʲ࠶ܝʳKotlin͔Βݺͼग़͢ͱɻɻɻ // ͜ΕίϯύΠϧΤϥʔʹͳΒͳ͍ val str = Reader.readNullableStringJava() // ͜ΕίϯύΠϧΤϥʔʹͳΒͳ͍ val
str : String? = Reader.readNullableStringJava() // ʂ͜ΕίϯύΠϧΤϥʔʹͳΒͳ͍ʂʂʂ val str : String = Reader.readNullableStringJava()
nullΛString?͡Όͳͯ͘ Stringܕͷมʹೖ͍ͯ͠Δ
͍ʂ
KotlinͰ NullPointerExcep/on ى͖·͢ʂ
͏গ͠ৄ͘͠ݴ͏ͱ
Javaͱͷڥք෦ͰదͳίʔυΛॻ͔ͳ͍ͱ ίϯύΠϧ௨Δ͚ΕͲ ࣮ߦ࣌ʹIllegalStateExcep,onNullPointerExcep,on͕ൃੜ
Τϥʔ2ύλʔϯ
ύλʔϯͦͷ1 IllegalStateExcep,on͕ൃੜ
Javaͷίʔυ public class Reader { public static String readNullableStringJava() {
return null; } } Kotlinͷίʔυ // ͜ͷೖͨ͋ͨ͠ΓͰIllegalStateExceptionൃੜ val str : String = Reader.readNullableStringJava()
ύλʔϯ1 Java͔Β͖ͨnullΛKotlinͷStringܕͷมʹೖͨ࣌͠Ͱ IllegalStateExcep,on͕ൃੜ
ύλʔϯͦͷ2 NullPointerExcep/on͕ൃੜ
Javaͷίʔυ public class Reader { public static String readNullableStringJava() {
return null; } } Kotlinͷίʔυ // ͜ͷೖΛͨ͋ͨ͠ΓͰϥϯλΠϜΤϥʔൃੜ͠ͳ͍ val str = Reader.readNullableStringJava() // ͜͜ͰNullPointerExceptionͰࢮ͵ val length = str.length
ύλʔϯ2 Java͔Β͖ͨnullΛKotlinͰܕΛ໌ࣔతʹએݴ͠ͳ͍มʹೖ ͦͯ͠ϝϯόΞΫηεͨ࣌͠ͰNullPointerExcep/onͰࢮ͵
Javaͱͷڥք෦ͰదͳίʔυΛॻ͔ͳ͍ͱ ίϯύΠϧ௨Δ͚ΕͲ ࣮ߦ࣌ʹIllegalStateExcep,onNullPointerExcep,on͕ൃੜ
ͲͬͪJava͔Βདྷͨnull͕ݪҼ JavaͷString͕KotlinͷString?͡Όͳ͍ ͔Β͜͏͍͏͜ͱʹ
ϙΠϯτϓϥοτϗʔϜܕʂ
JavaͰͷࢀরܕKotlinͰಛผѻ͍͞ΕΔ ͜Ε͕ϓϥοτϗʔϜܕ
ϓϥοτϗʔϜܕ • IDEͱ͔ͰString!ͱ͔ͬͯද͞ΕΔͭ • ίʔυ্ͰɺܕΛ໌ࣔతʹString!ͱॻ͚ͳ͍ • String!StringʹString?ʹೖͰ͖Δ • ਪ͞ΕͯString!ͰѻΘΕΔ߹͕͋Δ
Kotlin͔ΒݟΔͱฦܕ ϓϥοτϑΥʔϜܕͷString! public class Reader { public static String readNullableStringJava()
{ return null; } }
Kotlin͔Βݺͼग़͢ͱɻɻɻ // ԼͷstrϓϥοτϗʔϜܕͷString! // ͚ͲString!ͬͯ໌ࣔతʹॻ͚ͳ͍ val str = Reader.readNullableStringJava() //
String?ܕͷมʹೖ val str : String? = Reader.readNullableStringJava() // Stringܕͷมʹೖ val str : String = Reader.readNullableStringJava()
• String!ܕ(໌ࣔతʹॻ͔ͣਪ͞ΕΔ) • String?ܕ(໌ࣔతʹએݴ) • Stringܕ(໌ࣔతʹએݴ) ͦΕͧΕͷ߹Ͳ͏ͳΔʁ
ͦͷ1"String!ܕ(ϓϥοτϑΥʔϜܕ) // ԼϓϥοτϑΥʔϜܕͷString! val str = Reader.readNullableStringJava() // ίϯύΠϧΤϥʔͰͳ͍͠ɺ࣮ߦ࣌ΤϥʔʹͳΒͳ͍ println(str?.length)
// ίϯύΠϧΤϥʔʮͰʯͳ͍͚Ͳɺ // ࣮ߦ࣌ʹNullPointerExceptionൃੜ println(str.length)
ͦͷ2ɹString?ܕ // ԼStringܕ val str: String? = Reader.readNullableStringJava() // OK
println(str?.length) // ίϯύΠϧΤϥʔ println(str.length)
ͦͷ3ɹStringܕ // ԼStringܕ // ίϯύΠϧΤϥʔʹͳΒͳ͍ // ೖͨ͋ͨ͠ΓͰIllegalStateException͕ൃੜ val str: String
= Reader.readNullableStringJava() // ͦΕඞཁͳ͍Αͬͯɺܯࠂ͕ग़Δ println(str?.length) // OK println(str.length)
ϓϥοτϑΥʔϜܕΛ͔ͬ͠Γཧղʂ ಛʹܕΛ໌ࣔతʹએݴ͠ͳ͍Ͱ ϓϥοτϑΥʔϜܕͷ··ѻ͏߹ʹҙ
Javaͱͷڥք෦ͰదͳίʔυΛॻ͔ͳ͍ͱ ίϯύΠϧ௨Δ͚ΕͲ ࣮ߦ࣌ʹIllegalStateExcep,onNullPointerExcep,on͕ൃੜ
ͱΓ͋͑ͣString!ʢϓϥοτϑΥʔϜܕʣɺ String?Λ໌ࣔతʹએݴͨ͠มͰड͚͓͚ͯ ϥϯλΠϜΤϥʔൃੜ͠ͳ͍
ͱ͍͑
໌Β͔ʹnull͡Όͳ͍ͷ͋Δ ͍͍ͪͪString?Ͱड͚ΔͷΊΜͲ͍
͘͠Javaͷίʔυଆ͔Β String?Ͱड͚Δ͜ͱΛڧ੍͍ͨ͠
@NotNull'ͱ'@Nullable
Javaͷίʔυʹ@NotNullΛ͚ͭΔͱ KotlinͰTͱͯ͠ѻΘΕΔ @NotNull public static String readNotNull() { return "";
}
// IDE͕ɺฦΓܕStringͬͯڭ͑ͯ͘ΕΔʢString!Ͱͳ͍ʣ val str = Reader.readNotNull() // ͜ΕOK val str
: String = Reader.readNotNull() // ࣮ɺ͜ΕOK val str : String? = Reader.readNotNull()
Javaͷίʔυʹ@NullableΛ͚ͭΔͱ KotlinͰT?ͱͯ͠ѻΘΕΔ @Nulllable public static String readNullable() { return null;
}
// IDE͕ɺฦΓܕString?ͬͯڭ͑ͯ͘ΕΔʢString!Ͱͳ͍ʣ val str = Reader.readNullable() // ͜ΕOK val str
: String? = Reader.readNullable() // ͜ΕίϯύΠϧΤϥʔ val str : String = Reader.readNullable()
͋͠ͳ͕ͨJavaͷϥΠϒϥϦΛॻ͍͍ͯΔͳΒ ͥͻ@NotNullͱ@NullableΛదʹ͚ͭͯΈ͍ͯͩ͘͞ h"ps:/ /kotlinlang.org/docs/reference/java7interop.html#nullability7annota=ons
ͪͳΈʹ࣮ෳରԠ͍ͯ͠Δ • android.support.annota-on.Nullable • com.android.annota-ons.Nullable • org.eclipse.jdt.annota-on.Nullable • org.checkerframework.checker.nullness.qual.Nullable •
org.jetbrains.annota-ons.Nullable
ϙΠϯτ͓͞Β͍
ϙΠϯτ1 Javaͱͷڥք෦ͰదͳίʔυΛॻ͔ͳ͍ͱ ίϯύΠϧ௨Δ͚ΕͲ ࣮ߦ࣌ʹIllegalStateExcep,onNullPointerExcep,on͕ൃੜ
ϙΠϯτ2 ϓϥοτϑΥʔϜܕΛ͔ͬ͠Γཧղʂ ಛʹܕΛ໌ࣔతʹએݴ͠ͳ͍Ͱ ϓϥοτϑΥʔϜܕͷ··ѻ͏߹ʹҙ
ϙΠϯτ3 ͋͠ͳ͕ͨJavaͷϥΠϒϥϦΛॻ͍͍ͯΔͳΒ ͥͻ@NotNullͱ@NullableΛదʹ͚ͭͯΈ͍ͯͩ͘͞
Kotlin͍͍ݴޠͰ͢
Kotlinେ͖Ͱ͢
ࠓͷඪɺKotlinΛ࠷ۙ࢝Ίͨਓ͕ ʮKotlinͰNullPointerExcep0onͰΔ͡ΌΜʔʂʯ ͱ͍͏ࣄނΛ͙͜ͱͰͨ͠
ʮKotlinͩͬͯؒҧ͑ΔͱnullͰϋϚΔʂʯ ͜Ε͕1݅ͰݮΕ͏Ε͍͠Ͱ͢
͔͜͜Βઌ༨ஊͰ͢
ʮKotlinΛͻΖΊͯߦ͜͏ʂʯ ͬͯਓɺ͍ͯͩ͘͠͞
ʮnull$safetyͬͯԿͰ͔͢Ͷʁʯ
KotlinͰNullPointerExcep0on ൃੜ͠·͢ !!ͦ͏ͩ͠ɺॳظԽλΠϛϯάͰൃੜ͢Δ͜ͱ͋Δ
ʮnull$safetyʯ NullPointerExcep/on͕ൃੜ͠ͳ͍͜ͱ Ͱͳͯ͘
nullΛѻ͍͍͢ݴޠ༷ɾػೳʹΑΓ NullPointerExcep/on͕ൃੜͮ͠Β͍ ͜ͱͩͱࢥ͍ͬͯ·͢
KotlinͰ NullPointerExcep/on ى͖·͢
Kotlin͕nullʹ·ͭΘΔΛ ͍͍ײ͡ʹղܾͰ͖Δ͚ΕͲ ͜ͷϙΠϯτΕͳ͍Ͱ͍ͩ͘͞
ʮnull҆શݴޠͬͯԿͰ͔͢Ͷʁʯ
ʮnull҆શݴޠʯͱ͍͏ݴ༿Λ ϒϩάSNSهࣄͰΘͳ͍Α͏ʹ͍ͯ͠·͢
• ੩తܕ͚ݴޠ • ΫϥεϕʔεͷΦϒδΣΫτࢦݴޠ • JVMݴޠ ͜͏͍͏ݴ༿ͱҧͬͯ໌֬ͳఆٛͱ͔ ڞ௨ೝࣝͱ͔͕ͦ͜·Ͱͳ͍ͱࢥ͏ΜͰ͢Α
͏࣌ͥͻɺఆٛΛॻ͍ͯΈ͍ͯͩ͘͞ ͦͯͦ͠ͷࡍɺϓϥοτϑΥʔϜܕʹ͍ͭͯʂ
͓·͚1 ϓϥοτϑΥʔϜܕؔ࿈ͷίʔυ IDEAͰɺTools)>)Kotlin)>)Show)Kotlin)Bytecode ͯ͠ɺDecompile͢Δͱ໘ന͍ͷͰΈͯΈ͍ͯͩ͘͞
͓·͚2 ϓϥοτϑΥʔϜܕҎ֎Ͱ ΠϯελϯεͷॳظԽؔ࿈Ͱ NullPointerExcep/on͕ൃੜ