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
300
ChangeLogを読もう(1.2.70編)
sckm
1
310
3分でわかるSequence
sckm
1
640
略解reified
sckm
0
130
KDoc
sckm
1
750
Other Decks in Programming
See All in Programming
ONE WEDGE_company_guide
1wedge_one
0
470
スクラムガイドのスプリントレトロスペクティブを改めて読みかえしてみた / Re-reading the Sprint Retrospective Section in the Scrum Guide
mackey0225
3
410
大規模UIKitベースアプリへのTCAの段階的導入/gradual-adoption-of-tca-in-a-large-scale-uikit-based-app
takehilo
1
140
1BRC--Nerd Sniping the Java Community
gunnarmorling
0
340
二郎系ラーメンのコールで学ぶ AST 解析
memory1994
PRO
7
1.7k
Micro Frontends for Java Microservices - Devnexus 2024
mraible
PRO
0
490
educure_カリキュラム生操作マニュアル.pdf
linew_official
0
750
FigmaとPHPで作る1ミリたりとも表示崩れしない最強の帳票印刷ソリューション
ttskch
43
19k
コーンフレークから始める モデリング会話入門
ogurotakayuki
0
370
障害対応を起点としたもっといい開発と運用のサイクル作りのためにできること / Hatena Enginner Seminar #29
polamjag
0
110
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
380
MetricKitで予期せぬ終了を検知する話 / Detect unexpected termination with MetricKit
nekowen
1
180
Featured
See All Featured
Building Effective Engineering Teams - LeadDev
addyosmani
28
1.8k
YesSQL, Process and Tooling at Scale
rocio
164
13k
Atom: Resistance is Futile
akmur
259
25k
Designing for Performance
lara
601
67k
Creatively Recalculating Your Daily Design Routine
revolveconf
210
11k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
7
1k
Why Our Code Smells
bkeepers
PRO
331
56k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
241
1.2M
Building Your Own Lightsaber
phodgson
99
5.7k
What's in a price? How to price your products and services
michaelherold
237
11k
Git: the NoSQL Database
bkeepers
PRO
422
63k
Designing Experiences People Love
moore
136
23k
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͔Β͖࣋ͬͯͯίϛοτ