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
Mapped TypeとPlatform Typeの話
Search
RyotaMurohoshi
December 13, 2016
Technology
0
8.7k
Mapped TypeとPlatform Typeの話
2016/12/13(火)のSansanさんで開催された第4回 Kotlin Sansan勉強会での発表資料です。
RyotaMurohoshi
December 13, 2016
Tweet
Share
More Decks by RyotaMurohoshi
See All by RyotaMurohoshi
Unityの合同同人誌や合同商業誌を書いてる僕は感想やレビューや評価が欲しい
ryotamurohoshi
0
360
Unity 2021.1での Unityパッケージの名称変更について
ryotamurohoshi
0
630
Odin Validationはいいぞ!
ryotamurohoshi
2
850
Tilemapはいいぞ!2020 〜すごいぞ、プロジェクト専用拡張Brush〜
ryotamurohoshi
0
2.3k
Unityでも、新しいC#
ryotamurohoshi
0
1.2k
Riderはいいぞ!
ryotamurohoshi
1
3.2k
Riderのススメ〜俺はRiderここが好き〜
ryotamurohoshi
1
2.2k
Unity開発者に伝えたい.NETのこと
ryotamurohoshi
4
36k
ImportedLinqのススメ
ryotamurohoshi
0
1.3k
Other Decks in Technology
See All in Technology
形式手法の 10 メートル手前 #kernelvm / Kernel VM Study Hokuriku Part 7
ytaka23
5
820
ドメイン名の終活について - JPAAWG 7th -
mikit
32
19k
Terraform未経験の御様に対してどの ように導⼊を進めていったか
tkikuchi
2
370
なぜ今 AI Agent なのか _近藤憲児
kenjikondobai
2
1.2k
透過型SMTPプロキシによる送信メールの可観測性向上: Update Edition / Improved observability of outgoing emails with transparent smtp proxy: Update edition
linyows
2
200
Team Dynamicsを目指すウイングアーク1stのQAチーム
sadonosake
1
290
RubyのWebアプリケーションを50倍速くする方法 / How to Make a Ruby Web Application 50 Times Faster
hogelog
2
860
3次元点群データ「VIRTUAL SHIZUOKA』のオープンデータ化による恩恵と協働の未来/FOSS4G Japan 2024
kazz24s
0
140
State of Open Source Web Mapping Libraries
dayjournal
0
230
"君は見ているが観察していない"で考えるインシデントマネジメント
grimoh
4
1.1k
メールサーバ管理者のみ知る話
hinono
1
110
20241108_CS_LLMMT
shigashiyama
0
260
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Automating Front-end Workflow
addyosmani
1366
200k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
GitHub's CSS Performance
jonrohan
1030
460k
Typedesign – Prime Four
hannesfritz
40
2.4k
How STYLIGHT went responsive
nonsquared
95
5.2k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Docker and Python
trallard
40
3.1k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
Transcript
Mapped&TypeͱPla+orm&Typeͷ @RyotaMurohoshi 2016/12/13(Ր))ୈ4ճKotlinษڧձ)ˏ)Sansan
ࣗݾհ • ྄ࣨଠ(ΉΖ΄͠) • @RyotaMurohoshi • ࣄͰKotlin+Android
None
None
·ͣ؆୯ʹPla$orm(TypeͷΛͯ͠ ͦͷޙMapped&TypeͷΛ͠·͢ ʰ(Mutable)List<String!>!ʱܕͱ͔ͷ
ͬͦ͘͞ԣಓʹͦΕ·͕͢ String?String!ɺͳΜͯಡΈ·͢ʁ
͞Βͬͱཧղ͢ΔPla$orm(Type
ʰKotlinͩͬͨΒɺઈରNullPointerExep/onى͖ͳ͍ʱ ͜Εؒҧ͍ʂ
Kotlinͱnull // ίϯύΠϧΤϥʔ val string : String = null //
͜ΕOK val string : String? = null
String?Λฦ͕ؔ͋ͬͯ͢ fun loadMessage(): String? = /*ུ*/
// OK val message : String? = loadMessage() // ίϯύΠϧΤϥʔ
val message : String = loadMessage() // Nullableܕ?.Ͱͳ͍ͱϝϯόΞΫηεͰ͖ͳ͍ // ͠message͕nullͳΒmessage?.lengthnull println(message?.length) // ίϯύΠϧΤϥʔ println(message.length)
Kotlin͚ͩͳΒ ͦ͏ͦ͏NullPointerExcep/onى͖ͳ͍ ࣮Kotlin͚ͩͰNullPointerExcep0onग़ͤΔΒ͍͠
Java͔ΒདྷͨΦϒδΣΫτʁ
Java͔ΒདྷͨΦϒδΣΫτͷ͍ํΛؒҧ͑Δͱ NullPointerExcep/onͱ͔IllegalStateExcep/onʹͳΔ
Pla$orm(Type
ݪଇJava͔ΒདྷͨΦϒδΣΫτPla&orm*Type
Javaͷίʔυ public class Utility { public static String loadMessage() {
/* ུ */ } }
Kotlinͷίʔυ // IDEͩͱʮString!ʯͬͯදࣔ͞ΕΔ Platform Type val message = Utility.loadMessage() //
String?ܕʹɻString!String?ʹೖՄ val message : String? = Utility.loadMessage() // StringܕʹɻString!StringʹೖՄ val message : String = Utility.loadMessage()
ҙ
IllegalStateExcep,onʹͳΔྫ
IllegalStateExcep,onʹͳΔྫ // Platform TypeͷString!ܕΛStringܕʹೖͨ࣌͠ɺ // ͦͷΦϒδΣΫτ͕nullͳΒIllegalStateException val message: String =
Utility.loadMessage()
NullPointerExcep/onʹͳΔྫ
NullPointerExcep/onʹͳΔྫ // String!ܕ val message = Utility.loadMessage() // Platform TypeͷString!ܕͷϝϯόʹΞΫηεͨ࣌͠ɺ
// ͦͷΦϒδΣΫτ͕nullͩͬͨΒNullPointerException println(message.length) // ͜ΕͳΒOKɻnullʹͳΔ println(message?.length)
ίϯύΠϧΤϥʔʹͳΒͳͯ͘ ࣮ߦ࣌ʹΤϥʔʹͳΔ͔Βҙʂ
@Nullable@NotNullΛ͏·͓͘͏ʂ @NullableΛ͚ͭͨJavaͷStringString? @NotNullΛ͚ͭͨJavaͷStringString
ۦ͚ͰPla$orm(Type ৄ͘͠ɺQiitaͷΞυϕϯτΧϨϯμʔݟͯͶ
ͱ͜ΖͰPla$orm(TypeΫϥεܕͷ ͡Ό͋intͱ͔doubleͱ͔booleanͱ͔ɺ primi%veͲ͏ͳΔʁ
Mapped&Type
JavaͷintɾdoubleɾboolenͳͲ KotlinͰɺkotlin.Intɺkotlin.Doubleɺkotlin.BooleanʹͳΓ·͢
ݴ͍͑ΔͱJavaͷprimi(veɺ KotlinͰݻ༗ͷTypeͱͯ͠ѻΘΕΔ
Mapped&Type
(͜ΕυΩϡϝϯτʹॻ͍ͯ͋ͬͨ)
͡Ό͋JavaͷIntegerͱ͔Doubleͱ͔Boolenͱ͔ɺ primi%veܕͷϥούʔܕKotlin͔ΒͲ͏ݟ͑Δʁ (ͳ͔ͥυΩϡϝϯτʹͳ͔ͬͨ)
JavaͷInteger KotlinͰInt!
Javaͷίʔυ public class JInts { public static Integer getWrapperInteger() {
return /*ུ*/; } }
Kotlinͷίʔυ // numPlatform TypeͷInt!ܕ val num = JInts.getWrapperInteger() ѻ͍ํΛؒҧ͑ͨΒIllegalStateExcep,onʹͳͬͨΓɺNullPointerExcep,onʹͳͬͨΓ
ଞͷPla$orm(Typeͱಉ༷ʹ @Nullable@NotNullΛ͓͏
Javaͷίʔυ public class JInts { public static Integer getWrapperInteger() {
return /*ུ*/; } @Nullable public static Integer getWrapperIntegerNullable() { return /*ུ*/; } @NotNull public static Integer getWrapperIntegerNotNull() { return /*ུ*/; } }
Kotlinͷίʔυ // numPlatform TypeͷInt!ܕ val num = JInts.getWrapperInteger() // numInt?ܕ
val num = JInts.getWrapperIntegerNullable() // numIntܕ val num = JInts.getWrapperIntegerNotNull()
ͳΔ΄Ͳ
ͰٯʹɺKotlinͷInt?Double?Java͔ΒͲ͏ݟ͑Δʁ
KotlinͷInt? JavaͰInterger
Kotlinͷίʔυ class KInts { companion object { @JvmStatic fun getInt():
Int = 0 @JvmStatic fun getNullableInt(): Int? = 0 } }
Javaͷίʔυ public class KIntsUsage { public static void main(String[] args)
{ int num0 = KInts.getInt(); Integer num1 = KInts.getNullableInt(); // ΞϯϘΫγϯάͰ͜͏͔͚Δ int num2 = KInts.getNullableInt(); } }
KotlinͷInt? JavaͰInterger
ͳΔ΄Ͳ
Mapped&Type࣮primi,ve͚ͩ͡Όͳͯ͘ࢀরܕʹ͋Δ • java.lang.Enum,<.>,kotlin.Enum! • java.lang.Void,<.>,kotlin.Nothing! • java.lang.String,<.>,kotlin.String! • java.lang.Number,<.>,kotlin.Number! ࣮Stringʂʂʂ
࠷ޙʹ
ʰ(Mutable)List<T>!ʱͱʁ
Mapped&TypeͱPla+orm&Typeͷ߹Θٕͤ
Kotlinͷ 2ͭͷϦετΠϯλʔϑΣʔε
kotlin.collec*ons.List<T> count()ͱ͔ཁૉऔಘͱ͔ReadOnlyͳϝιου͔͠ͳ͍ (ͪͳΈʹList<T>ΠϛϡʔλϒϧͰͳ͍)
kotlin.collec*ons.MutableList<T> List<T>Λܧঝ͠͞ΒʹཁૉΛมߋ͢ΔϝιουΛ࣋ͭ (clearͱ͔addͱ͔Ͱ͖Δ)
ͲͪΒJavaͰ java.u&l.List<T>
Kotlinͷίʔυ class KLists { companion object { @JvmStatic fun getStringList():
List<String> = listOf("") @JvmStatic fun getMutableStringList(): MutableList<String> = mutableListOf("") } }
Javaͷίʔυ List<String> list0 = KLists.getStringList(); List<String> list1 = KLists.getMutableStringList();
ͰJavaͷjava.u'l.List<String> Kotlin͔ΒͲ͏ݟ͑Δ?
Javaͷίʔυ public class JLists { public static List<String> getList() {
return new ArrayList<>(); } }
Kotlinͷίʔυ // IDEͰͷදࣔʰ(Mutable)List<String!>!ʱܕ val list = JLists.getList() println(list.count) // MutableListͷϝιου͕asͳ͠ͰݺΔ
list.clear() println(list.count)
MutableList͡Όͳͯ͘Listͱͯ͠ѻ͍͍ͨ߹ʁ
@ReadOnly
@ReadOnlyͱ@MutableΛ༩ͨ͠Javaͷίʔυ(@NotNull) public class JLists { @NotNull @ReadOnly public static List<String>
getReadonlyList() { return new ArrayList<>(); } @NotNull @Mutable public static List<String> getMutableList() { return new ArrayList<>(); } }
Kotlinͷίʔυ // kotlin.collections.List<String!> val list = JLists.getReadonlyList() // ͜ΕOK val
list : List<String> = JLists.getReadonlyList() // ίϯύΠϧΤϥʔ val list : MutableList<String> = JLists.getReadonlyList() println(list.count()) // OK list.clear() // ίϯύΠϧΤϥʔ @ReadOnlyͱ@MutableΛ͚ͭΔ͜ͱͰมԽ
// kotlin.collections.MutableList<String!> val list = JLists.getMutableList() // ͜ΕOK val list
: MutableList<String> = JLists.getMutableList() println(list.count()) // OK list.clear() // OK // MutableListListΛܧঝ͍ͯ͠ΔͷͰɺͭ͜͡ΕOK // ͨͩclearͱ͔addݺͳ͍ // val list : List<String> = JLists.getMutableList() @ReadOnlyͱ@MutableΛ͚ͭΔ͜ͱͰมԽ
͜ΕΒList<T>͚ͩͰͳ͘ Set<T>Map<T>Ͱ͍ͬ͠ΐ
·ͱΊ Pla$orm(TyoeͱMapped(Type ཧղ͓ͯ͜͠͏ʂ