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
110
Hyperion Item Nameplate
sckm
0
140
[紹介]Writing Your First Kotlin Compiler Plugin by Kevin Most
sckm
0
340
ChangeLogを読もう(1.2.70編)
sckm
1
360
3分でわかるSequence
sckm
1
710
略解reified
sckm
0
130
KDoc
sckm
1
830
Other Decks in Programming
See All in Programming
PostgreSQLのRow Level SecurityをPHPのORMで扱う Eloquent vs Doctrine #phpcon #track2
77web
2
490
エンジニア向け採用ピッチ資料
inusan
0
180
XP, Testing and ninja testing
m_seki
3
220
システム成長を止めない!本番無停止テーブル移行の全貌
sakawe_ee
1
160
20250704_教育事業におけるアジャイルなデータ基盤構築
hanon52_
5
550
GitHub Copilot and GitHub Codespaces Hands-on
ymd65536
2
140
スタートアップの急成長を支えるプラットフォームエンジニアリングと組織戦略
sutochin26
0
1.9k
「Cursor/Devin全社導入の理想と現実」のその後
saitoryc
0
720
LT 2025-06-30: プロダクトエンジニアの役割
yamamotok
0
680
明示と暗黙 ー PHPとGoの インターフェイスの違いを知る
shimabox
2
470
Node-RED を(HTTP で)つなげる MCP サーバーを作ってみた
highu
0
120
新メンバーも今日から大活躍!SREが支えるスケールし続ける組織のオンボーディング
honmarkhunt
3
3.9k
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
The Invisible Side of Design
smashingmag
301
51k
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.8k
Adopting Sorbet at Scale
ufuk
77
9.4k
How to train your dragon (web standard)
notwaldorf
94
6.1k
Stop Working from a Prison Cell
hatefulcrawdad
270
20k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.8k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
17
950
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
138
34k
Gamification - CAS2011
davidbonilla
81
5.3k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
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͔Β͖࣋ͬͯͯίϛοτ