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
Property + Getter
Search
scache
July 03, 2018
Programming
0
1.3k
Property + Getter
Kotlin
Property + Getter
scache
July 03, 2018
Tweet
Share
More Decks by scache
See All by scache
Hyperion Item Nameplate
sckm
0
130
[紹介]Writing Your First Kotlin Compiler Plugin by Kevin Most
sckm
0
310
ChangeLogを読もう(1.2.70編)
sckm
1
330
3分でわかるSequence
sckm
1
660
略解reified
sckm
0
130
KDoc
sckm
1
770
Other Decks in Programming
See All in Programming
Mastering AsyncSequence - 使う・作る・他のデザインパターン(クロージャ、Delegate など)から移行する
treastrain
3
1.4k
プログラマのための音楽入門
cheebow
4
540
Boost Your Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
1
970
What is Parser
yui_knk
4
490
メモリ最適化を究める!iOSアプリ開発における5つの重要なポイント
yhirakawa333
0
370
Wallet API, Verifier APIで実現するIDカード on iPhoneの世界
shitamori1272
1
320
ESLint Rule により事業, 技術ドメインに沿った制約と誓約を敷衍させるアプローチのすゝめ
shinyaigeek
1
2.7k
『ドメイン駆動設計をはじめよう』中核の業務領域
masuda220
PRO
5
850
【TID2024】模擬講義:プログラマと一緒にゲームをデザインしてみよう!
akatsukigames_tech
0
390
大公開!iOS開発の悩みトップ5 〜iOSDC Japan 2024〜
ryunakayama
0
180
Swiftコードバトル必勝法
toshi0383
0
140
フロントエンドカンファレンス北海道2024 『小規模サイトでも使えるVite 〜HTMLコーディングをよりスマートに〜』長谷川広武(ハム)
h2ham
1
2.5k
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1365
200k
Building an army of robots
kneath
302
42k
A Tale of Four Properties
chriscoyier
155
22k
Side Projects
sachag
451
42k
Building Better People: How to give real-time feedback that sticks.
wjessup
359
18k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
52k
Thoughts on Productivity
jonyablonski
65
4.2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
248
20k
Being A Developer After 40
akosma
82
580k
How GitHub (no longer) Works
holman
309
140k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
109
6.9k
Rails Girls Zürich Keynote
gr2m
93
13k
Transcript
Property + Getter ू·ΕKotlin͖ʂKotlinѪձ vol2 scache( @scal_ch )
About me • scache ( @scal_ch ) • Android/Kotlin •
AbemaTV
val + getter vs fun val isEmpty get() = size
== 0 or fun isEmpty() = size == 0
Properties • var/val • var/val + custom getter • Property
Delegation(e.g. lazy)
var (not null) class Var(var varHoge: String = “var”) Var().let
{ it.varHoge.toList() }
val(not null) class Val(val valHoge: String = "not null") Val().let
{ it.valHoge.toList() }
var class Var(var varHoge: String? = “var”) Var().let { it.varHoge
?: return@let it.varHoge toList() }
var class Var(var varHoge: String? = “var”) Var().let { it.varHoge
?: return@let it.varHoge?.toList() }
val class Val(val valHoge: String? = "not null") Val().let {
it.valHoge ?: return@let it.valHoge toList() }
val class Val(val valHoge: String? = "not null") Val().let {
it.valHoge ?: return@let it.valHoge.toList() }
var + custom getter class VarGetter { val rand =
Random() var varGetter: String? = null get() = "random: ${rand.nextInt()}" } VarGetter().let { it.varGetter ?: return@let it.varGetter toList() }
var + custom getter class VarGetter { val rand =
Random() var varGetter: String? = null get() = "random: ${rand.nextInt()}" } VarGetter().let { it.varGetter ?: return@let it.varGetter?.toList() }
val + custom getter class ValGetter( var hoge: String? =
"not null" ) { val valNullable: String? get() = hoge } ValGetter().let { it.valNullable ?: return@let it.valNullable toList() }
val + custom getter class ValGetter( var hoge: String? =
"not null" ) { val valNullable: String? get() = hoge } ValGetter().let { it.valNullable ?: return@let it.valNullable?.toList() }
Declaring Properties These can be declared as mutable, using the
var keyword or read-only using the val keyword. https://kotlinlang.org/docs/reference/properties.html#declaring-properties
Declaring Properties These can be declared as mutable, using the
var keyword or read-only using the val keyword. https://kotlinlang.org/docs/reference/properties.html#declaring-properties
val + lazy class LazyVal { val hoge: String? by
lazy { "not null" } } LazyVal().let { it.hoge ?: return@let it.hoge toList() }
val + lazy class LazyVal { val hoge: String? by
lazy { "not null" } } LazyVal().let { it.hoge ?: return@let it.hoge?.toList() }
val + getter vs fun val isEmpty get() = size
== 0 or fun isEmpty() = size == 0
Kotlin Πϯ ΞΫγϣϯʹΑΔͱ Ҿͷͳ͍ؔͱΧελϜGetterΛ࣋ͭϓϩύςΟՄಡੑ ʹͷΈҧ͍͕͋Δ
Kotlin Πϯ ΞΫγϣϯʹΑΔͱ Ҿͷͳ͍ؔͱΧελϜGetterΛ࣋ͭϓϩύςΟՄಡੑ ʹͷΈҧ͍͕͋Δ hoge.isEmpty or hoge.isEmpty()
Kotlin Πϯ ΞΫγϣϯʹΑΔͱ Ҿͷͳ͍ؔͱΧελϜGetterΛ࣋ͭϓϩύςΟՄಡੑ ʹͷΈҧ͍͕͋Δ ΫϥεͷಛΛදݱ͍ͨ͠߹ʹϓϩύςΟʹ͢Δ
kotlin-stdlib public val <T> List<T>.lastIndex: Int get() = this.size -
1
Functions vs Properties Prefer a property one a function when
the underlying algorithm • does not throw • is cheap to calculate • returns the same result over innovations if the object state hasn’t changed https://kotlinlang.org/docs/reference/coding-conventions.html#functions-vs-properties
·ͱΊ ݸਓతͳҙݟ • valมͷ࣮Λݟͳ͍ͱ͕ෆม͔Ͳ͏͔ෆ໌ • Functions vs Properties ͷϧʔϧʹै͏ •
໎ͬͨΒfun • ผͷҙݟฉ͖͍ͨͷͰ࠙ձͰ͓ئ͍͠·͢ʂ
None
͓·͚ J2K Branch A 1. javaϑΝΠϧΛίϐʔͯ͠ktϑΝΠϧΛ࡞ 2. ktϑΝΠϧΛमਖ਼ 3. javaϑΝΠϧΛফͯ͠ίϛοτ
Branch B 1. javaϑΝΠϧΛktϑΝΠϧʹϦωʔϜͯ͠ίϛοτ 2. ktϑΝΠϧͷ༰ΛBranchA͔Β͖࣋ͬͯͯίϛοτ