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. Property + Getter
    ू·ΕKotlin޷͖ʂKotlinѪ޷ձ vol2
    scache( @scal_ch )

    View Slide

  2. About me
    • scache ( @scal_ch )

    • Android/Kotlin

    • AbemaTV

    View Slide

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

    View Slide

  4. Properties
    • var/val

    • var/val + custom getter

    • Property Delegation(e.g. lazy)

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    hoge.isEmpty
    or
    hoge.isEmpty()

    View Slide

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

    Ϋϥεͷಛ௃Λදݱ͍ͨ͠৔߹ʹ͸ϓϩύςΟʹ͢Δ

    View Slide

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

    View Slide

  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

    View Slide

  25. ·ͱΊ
    ݸਓతͳҙݟ

    • valม਺ͷ࣮૷Λݟͳ͍ͱ஋͕ෆม͔Ͳ͏͔ෆ໌

    • Functions vs Properties ͷϧʔϧʹै͏

    • ໎ͬͨΒfun

    • ผͷҙݟ΋ฉ͖͍ͨͷͰ࠙਌ձͰ͓ئ͍͠·͢ʂ

    View Slide

  26. View Slide

  27. ͓·͚ J2K
    Branch A

    1. javaϑΝΠϧΛίϐʔͯ͠ktϑΝΠϧΛ࡞੒

    2. ktϑΝΠϧΛमਖ਼

    3. javaϑΝΠϧΛফͯ͠ίϛοτ

    Branch B

    1. javaϑΝΠϧΛktϑΝΠϧʹϦωʔϜͯ͠ίϛοτ

    2. ktϑΝΠϧͷ಺༰ΛBranchA͔Β͖࣋ͬͯͯίϛοτ

    View Slide