Slide 1

Slide 1 text

Property + Getter ू·ΕKotlin޷͖ʂKotlinѪ޷ձ vol2 scache( @scal_ch )

Slide 2

Slide 2 text

About me • scache ( @scal_ch ) • Android/Kotlin • AbemaTV

Slide 3

Slide 3 text

val + getter vs fun val isEmpty get() = size == 0 or fun isEmpty() = size == 0

Slide 4

Slide 4 text

Properties • var/val • var/val + custom getter • Property Delegation(e.g. lazy)

Slide 5

Slide 5 text

var (not null) class Var(var varHoge: String = “var”) Var().let { it.varHoge.toList() }

Slide 6

Slide 6 text

val(not null) class Val(val valHoge: String = "not null") Val().let { it.valHoge.toList() }

Slide 7

Slide 7 text

var class Var(var varHoge: String? = “var”) Var().let { it.varHoge ?: return@let it.varHoge toList() }

Slide 8

Slide 8 text

var class Var(var varHoge: String? = “var”) Var().let { it.varHoge ?: return@let it.varHoge?.toList() }

Slide 9

Slide 9 text

val class Val(val valHoge: String? = "not null") Val().let { it.valHoge ?: return@let it.valHoge toList() }

Slide 10

Slide 10 text

val class Val(val valHoge: String? = "not null") Val().let { it.valHoge ?: return@let it.valHoge.toList() }

Slide 11

Slide 11 text

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() }

Slide 12

Slide 12 text

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() }

Slide 13

Slide 13 text

val + custom getter class ValGetter( var hoge: String? = "not null" ) { val valNullable: String? get() = hoge } ValGetter().let { it.valNullable ?: return@let it.valNullable toList() }

Slide 14

Slide 14 text

val + custom getter class ValGetter( var hoge: String? = "not null" ) { val valNullable: String? get() = hoge } ValGetter().let { it.valNullable ?: return@let it.valNullable?.toList() }

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

val + lazy class LazyVal { val hoge: String? by lazy { "not null" } } LazyVal().let { it.hoge ?: return@let it.hoge toList() }

Slide 18

Slide 18 text

val + lazy class LazyVal { val hoge: String? by lazy { "not null" } } LazyVal().let { it.hoge ?: return@let it.hoge?.toList() }

Slide 19

Slide 19 text

val + getter vs fun val isEmpty get() = size == 0 or fun isEmpty() = size == 0

Slide 20

Slide 20 text

Kotlin Πϯ ΞΫγϣϯʹΑΔͱ Ҿ਺ͷͳ͍ؔ਺ͱΧελϜGetterΛ࣋ͭϓϩύςΟ͸Մಡੑ ʹͷΈҧ͍͕͋Δ

Slide 21

Slide 21 text

Kotlin Πϯ ΞΫγϣϯʹΑΔͱ Ҿ਺ͷͳ͍ؔ਺ͱΧελϜGetterΛ࣋ͭϓϩύςΟ͸Մಡੑ ʹͷΈҧ͍͕͋Δ hoge.isEmpty or hoge.isEmpty()

Slide 22

Slide 22 text

Kotlin Πϯ ΞΫγϣϯʹΑΔͱ Ҿ਺ͷͳ͍ؔ਺ͱΧελϜGetterΛ࣋ͭϓϩύςΟ͸Մಡੑ ʹͷΈҧ͍͕͋Δ Ϋϥεͷಛ௃Λදݱ͍ͨ͠৔߹ʹ͸ϓϩύςΟʹ͢Δ

Slide 23

Slide 23 text

kotlin-stdlib public val List.lastIndex: Int get() = this.size - 1

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

·ͱΊ ݸਓతͳҙݟ • valม਺ͷ࣮૷Λݟͳ͍ͱ஋͕ෆม͔Ͳ͏͔ෆ໌ • Functions vs Properties ͷϧʔϧʹै͏ • ໎ͬͨΒfun • ผͷҙݟ΋ฉ͖͍ͨͷͰ࠙਌ձͰ͓ئ͍͠·͢ʂ

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

͓·͚ J2K Branch A 1. javaϑΝΠϧΛίϐʔͯ͠ktϑΝΠϧΛ࡞੒ 2. ktϑΝΠϧΛमਖ਼ 3. javaϑΝΠϧΛফͯ͠ίϛοτ Branch B 1. javaϑΝΠϧΛktϑΝΠϧʹϦωʔϜͯ͠ίϛοτ 2. ktϑΝΠϧͷ಺༰ΛBranchA͔Β͖࣋ͬͯͯίϛοτ