Skip to content

Latest commit

 

History

History
25 lines (18 loc) · 659 Bytes

by-lazy.md

File metadata and controls

25 lines (18 loc) · 659 Bytes

Lazy delegate

lazy() is a function that takes a lambda and returns an instance of Lazy which can serve as a delegate for implementing a lazy property: the first call to get() executes the lambda passed to lazy() and remembers the result, subsequent calls to get() simply return the remembered result.

val lazyValue: String by lazy {
    println("computed!")
    "Hello"
}

fun main() {
    println(lazyValue)
    println(lazyValue)
}

computed!
Hello
Hello

References

Delegated Properties

https://stackoverflow.com/a/36623703