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.4k
Property + Getter
Kotlin
Property + Getter
scache
July 03, 2018
Tweet
Share
More Decks by scache
See All by scache
ExoPlayerのトラック選択と再生中の解像度制限
sckm
0
99
Hyperion Item Nameplate
sckm
0
140
[紹介]Writing Your First Kotlin Compiler Plugin by Kevin Most
sckm
0
340
ChangeLogを読もう(1.2.70編)
sckm
1
350
3分でわかるSequence
sckm
1
700
略解reified
sckm
0
130
KDoc
sckm
1
810
Other Decks in Programming
See All in Programming
今話題のMCPサーバーをFastAPIでサッと作ってみた
yuukis
0
130
実践Webフロントパフォーマンスチューニング
cp20
45
10k
一緒に働きたくなるプログラマの思想 #QiitaConference
mu_zaru
82
21k
AI時代のリアーキテクチャ戦略 / Re-architecture Strategy in the AI Era
dachi023
0
110
The Nature of Complexity in John Ousterhout’s Philosophy of Software Design
philipschwarz
PRO
0
170
インプロセスQAにおいて大事にしていること / In-process QA Meetup
medley
0
170
파급효과: From AI to Android Development
l2hyunwoo
0
170
Flutterでllama.cppをつかってローカルLLMを試してみた
sakuraidayo
0
150
The Missing Link in Angular’s Signal Story: Resource API and httpResource
manfredsteyer
PRO
0
150
OpenTelemetry + LLM = OpenLLMetry!?
yunosukey
1
160
In geheimer Mission: AI Agents entwickeln
joergneumann
0
120
ASP.NETアプリケーションのモダナイゼーションについて
tomokusaba
0
260
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.8k
Build The Right Thing And Hit Your Dates
maggiecrowley
35
2.7k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
How STYLIGHT went responsive
nonsquared
100
5.5k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
120
52k
StorybookのUI Testing Handbookを読んだ
zakiyama
30
5.7k
Become a Pro
speakerdeck
PRO
28
5.3k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
179
53k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
52
2.5k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
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͔Β͖࣋ͬͯͯίϛοτ