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.8k
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
420
Unity 2021.1での Unityパッケージの名称変更について
ryotamurohoshi
0
640
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
なぜCodeceptJSを選んだか
goataka
0
160
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
310
バクラクのドキュメント解析技術と実データにおける課題 / layerx-ccc-winter-2024
shimacos
2
1.1k
LINEヤフーのフロントエンド組織・体制の紹介【24年12月】
lycorp_recruit_jp
0
530
レンジャーシステムズ | 会社紹介(採用ピッチ)
rssytems
0
150
生成AIのガバナンスの全体像と現実解
fnifni
1
180
NW-JAWS #14 re:Invent 2024(予選落ち含)で 発表された推しアップデートについて
nagisa53
0
260
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
宇宙ベンチャーにおける最近の情シス取り組みについて
axelmizu
0
110
AWS re:Invent 2024で発表された コードを書く開発者向け機能について
maruto
0
190
非機能品質を作り込むための実践アーキテクチャ
knih
3
1.1k
Amazon SageMaker Unified Studio(Preview)、Lakehouse と Amazon S3 Tables
ishikawa_satoru
0
150
Featured
See All Featured
How to Ace a Technical Interview
jacobian
276
23k
The Pragmatic Product Professional
lauravandoore
32
6.3k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
0
97
The Language of Interfaces
destraynor
154
24k
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
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 ཧղ͓ͯ͜͠͏ʂ