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

Property + Getter

scache
July 03, 2018

Property + Getter

Kotlin
Property + Getter

scache

July 03, 2018
Tweet

More Decks by scache

Other Decks in Programming

Transcript

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

    == 0 or fun isEmpty() = size == 0
  2. val class Val(val valHoge: String? = "not null") Val().let {

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

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

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

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

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

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

    == 0 or fun isEmpty() = size == 0
  13. 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
  14. ͓·͚ J2K Branch A 1. javaϑΝΠϧΛίϐʔͯ͠ktϑΝΠϧΛ࡞੒ 2. ktϑΝΠϧΛमਖ਼ 3. javaϑΝΠϧΛফͯ͠ίϛοτ

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