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
670
略解reified
sckm
0
130
KDoc
sckm
1
780
Other Decks in Programming
See All in Programming
Quarto Clean Theme
nicetak
0
220
利用者視点で考える、イテレータとの上手な付き合い方
syumai
4
210
Rails 8 Frontend: 10 commandments & 7 deadly sins in 2025
yshmarov
1
620
Cohesion in Modeling and Design
mploed
3
190
Cloud Adoption Framework にみる組織とクラウド導入戦略
tomokusaba
2
370
Compose Multiplatform과 Ktor로 플랫폼의 경계를 넘어보자
kwakeuijin
0
250
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
250
RemixとCloudflare Stack におけるFile Upload
ossamoon
1
120
上手に付き合うコンポーネントテスト
quramy
3
1.2k
Iteratorでページネーションを実現する
sonatard
3
700
Modern Functional Fluent CFML REST by Luis Majano
ortus24
0
130
CSC305 Lecture 01
javiergs
PRO
1
140
Featured
See All Featured
Optimizing for Happiness
mojombo
375
69k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2k
Building Adaptive Systems
keathley
37
2.1k
Six Lessons from altMBA
skipperchong
26
3.4k
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
Robots, Beer and Maslow
schacon
PRO
157
8.2k
We Have a Design System, Now What?
morganepeng
49
7.1k
Music & Morning Musume
bryan
46
6.1k
The Art of Programming - Codeland 2020
erikaheidi
50
13k
Building Better People: How to give real-time feedback that sticks.
wjessup
360
19k
Git: the NoSQL Database
bkeepers
PRO
425
64k
A designer walks into a library…
pauljervisheath
201
24k
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͔Β͖࣋ͬͯͯίϛοτ