Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Mapped TypeとPlatform Typeの話
Search
RyotaMurohoshi
December 13, 2016
Technology
9.7k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Mapped TypeとPlatform Typeの話
2016/12/13(火)のSansanさんで開催された第4回 Kotlin Sansan勉強会での発表資料です。
RyotaMurohoshi
December 13, 2016
More Decks by RyotaMurohoshi
See All by RyotaMurohoshi
Tilemapのアップデートについて
ryotamurohoshi
0
100
Unityの合同同人誌や合同商業誌を書いてる僕は感想やレビューや評価が欲しい
ryotamurohoshi
0
630
Unity 2021.1での Unityパッケージの名称変更について
ryotamurohoshi
0
920
Odin Validationはいいぞ!
ryotamurohoshi
2
1.1k
Tilemapはいいぞ!2020 〜すごいぞ、プロジェクト専用拡張Brush〜
ryotamurohoshi
0
2.6k
Unityでも、新しいC#
ryotamurohoshi
0
1.5k
Riderはいいぞ!
ryotamurohoshi
1
3.8k
Riderのススメ〜俺はRiderここが好き〜
ryotamurohoshi
1
2.8k
Unity開発者に伝えたい.NETのこと
ryotamurohoshi
4
38k
Other Decks in Technology
See All in Technology
初対面のメンバーがチームを組み、Claude Codeでゼロからサービスをリリース
mkdev10
1
120
IoTデバイスを繋げるプロトコルMatterをAndroidで使用する方法
harutiro
0
140
"優秀な誰か"の下位互換にならないために
yut0naga1_fa
1
1.7k
契約書画像からの情報抽出に特化した視覚接地モデルを育てる話
sansantech
PRO
1
160
OSC2026on_the-world-is-waiting-for-your-voice.pdf
naruoga
0
370
ai_cording_with_k8s_knowledge.pdf
mochizuki875
1
330
Execution in the Kingdom of Agents: Reflections on Abstraction and Complexity
bcantrill
0
940
二歩目のフロントエンド・ウェブアクセシビリティ
camiha
2
370
Cognition AI Japan - ウェビナー2_テストQA_運用監視
cogakira
0
130
FOSS4Gを活用した自治体業務DXの試み
470715
0
150
「事実」を「計測」して、「解釈」する 〜AWS Lambdaのメモリ最適解の見つけ方〜
maimyyym
0
130
Deep Data Security 機能解説
oracle4engineer
PRO
2
770
Featured
See All Featured
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.3k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
580
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
300
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.5k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7k
Technical Leadership for Architectural Decision Making
baasie
3
600
The agentic SEO stack - context over prompts
schlessera
0
970
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.9k
Claude Code のすすめ
schroneko
67
230k
Designing Experiences People Love
moore
143
24k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
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 ཧղ͓ͯ͜͠͏ʂ