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
640
Unity 2021.1での Unityパッケージの名称変更について
ryotamurohoshi
0
930
Odin Validationはいいぞ!
ryotamurohoshi
2
1.1k
Tilemapはいいぞ!2020 〜すごいぞ、プロジェクト専用拡張Brush〜
ryotamurohoshi
0
2.6k
Unityでも、新しいC#
ryotamurohoshi
0
1.6k
Riderはいいぞ!
ryotamurohoshi
1
3.9k
Riderのススメ〜俺はRiderここが好き〜
ryotamurohoshi
1
2.8k
Unity開発者に伝えたい.NETのこと
ryotamurohoshi
4
38k
Other Decks in Technology
See All in Technology
レガシーコードに対する loop engineering的なAI開発プロセス導入について
kubell_hr
0
280
属人化したISMS運用をGitHubとAIで仕組み化した1年間の記録
sioncojp
0
310
増え続けるプロダクトの品質をどう担保するか。ProductSpecとTypeScriptによる取り組み
joere
3
1.1k
ゼロから作るWebアプリの新定番!?Amplify Gen 2 × AWS Blocksで実現する「開発速度とRDBの柔軟性のいいとこ取り」
amixedcolor
1
170
Kiro Crew 入門 (手を離しても進む仕事) | 目指せ Kiro マスター 11
inariku
PRO
4
510
Metadata - Section 3 : Dublin Core Standard · DC
mohab1
0
110
続・ソフトウェアアーキテクトのための意思決定術: Decision Readiness, Revisited — When AI Changes the Economics of Software Architecture
snoozer05
PRO
9
2.7k
生成AI時代のIAMのマニアックな話
takuros
6
2.7k
業務でAWSをあまり使っていない私が、AWSへのモチベを保つ方法
smt7174
0
180
An Introduction to Context Ontology Accelerator (teaching AI agents what your data means)
inariku
PRO
1
120
PostgreSQL オプティマイザ統計の秘密
nori_shinoda
0
400
新米を「売り越さない」、Aurora DSQLで作る在庫ゼロ死守の限定ドロップEC
yama3133
0
190
Featured
See All Featured
Making Projects Easy
brettharned
120
6.8k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
320
Google's AI Overviews - The New Search
badams
0
1.7k
Believing is Seeing
oripsolob
1
270
The browser strikes back
jonoalderson
0
1.9k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3.1k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
610
How GitHub (no longer) Works
holman
316
150k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
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 ཧղ͓ͯ͜͠͏ʂ