kotlinlazy-initialization

What are Lazy Properties good for? What's their aim?


What is the purpose, respectively the aim, of a lazy property?

Kotlin-language documentation: https://kotlinlang.org/docs/delegated-properties.html#lazy-properties

Saving memory? Or is it something else?


Solution

  • Could one or more of several related factors:

    Some of these reasons are purely about efficiency.  For those, you'd need to consider factors such as how likely the value is to be needed at all, how long it might take to generate, whether a delay at construction is more or less important than a delay when the value is first used, and so on.

    But other reasons can affect whether and how the program runs, and so for these, laziness could be a necessity.

    Either way, having an easy, concise, robust way to implement lazy properties is a very useful addition to Kotlin.  (Especially as it can be done in the standard library, without affecting the language itself.)