elye
12/10/2022, 7:35 AMAdd 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
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
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 🙏elye
12/10/2022, 7:43 AMColin Marsch
12/10/2022, 3:16 PMlazy
works under the hood by having you implement it yourself