Hi All, I'm going through <Koan's Lazy Property example>. The question is ```Add a custom getter to...
e
Hi All, I'm going through Koan's Lazy Property example. The question is
Copy code
Add a custom getter to make the val lazy really lazy. It should be initialized by invoking initializer() during the first access.
You can add any additional properties as you need.
Do not use delegated properties!
The answer code is
Copy code
class LazyProperty(val initializer: () -> Int) {
    var value: Int? = null
    val lazy: Int
        get() {
            if (value == null) {
                value = initializer()
            }
            return value!!
        }
}
I tried the below and it also pass
Copy code
class LazyProperty(val initializer: () -> Int) {
    val lazy by lazy {
        initializer()
    }
}
I'm not sure the intent of this tutorial introducing
lazy
, but not using
by lazy
. Is it to show us the
get()
instead? Thanks 🙏
Oh, I got it, because the next example will show us how to do so https://play.kotlinlang.org/koans/Properties/Delegates%20examples/Task.kt
c
This seems to be intending to show how
lazy
works under the hood by having you implement it yourself