Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Property + Getter

07cc3901ba91cc4fc0fee0d26944e314?s=47 scache
July 03, 2018

Property + Getter

Kotlin
Property + Getter

07cc3901ba91cc4fc0fee0d26944e314?s=128

scache

July 03, 2018
Tweet

Transcript

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

  2. About me • scache ( @scal_ch ) • Android/Kotlin •

    AbemaTV
  3. val + getter vs fun val isEmpty get() = size

    == 0 or fun isEmpty() = size == 0
  4. Properties • var/val • var/val + custom getter • Property

    Delegation(e.g. lazy)
  5. var (not null) class Var(var varHoge: String = “var”) Var().let

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

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

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

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

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

    it.valHoge ?: return@let it.valHoge.toList() }
  11. 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() }
  12. 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() }
  13. val + custom getter class ValGetter( var hoge: String? =

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

    "not null" ) { val valNullable: String? get() = hoge } ValGetter().let { it.valNullable ?: return@let it.valNullable?.toList() }
  15. 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
  16. 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
  17. val + lazy class LazyVal { val hoge: String? by

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

    lazy { "not null" } } LazyVal().let { it.hoge ?: return@let it.hoge?.toList() }
  19. val + getter vs fun val isEmpty get() = size

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

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

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

  23. kotlin-stdlib public val <T> List<T>.lastIndex: Int get() = this.size -

    1
  24. 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
  25. ·ͱΊ ݸਓతͳҙݟ • valม਺ͷ࣮૷Λݟͳ͍ͱ஋͕ෆม͔Ͳ͏͔ෆ໌ • Functions vs Properties ͷϧʔϧʹै͏ •

    ໎ͬͨΒfun • ผͷҙݟ΋ฉ͖͍ͨͷͰ࠙਌ձͰ͓ئ͍͠·͢ʂ
  26. None
  27. ͓·͚ J2K Branch A 1. javaϑΝΠϧΛίϐʔͯ͠ktϑΝΠϧΛ࡞੒ 2. ktϑΝΠϧΛमਖ਼ 3. javaϑΝΠϧΛফͯ͠ίϛοτ

    Branch B 1. javaϑΝΠϧΛktϑΝΠϧʹϦωʔϜͯ͠ίϛοτ 2. ktϑΝΠϧͷ಺༰ΛBranchA͔Β͖࣋ͬͯͯίϛοτ