Lazy

interface Lazy<out T>(source)

Represents a value with lazy initialization.

To create an instance of Lazy use the lazy function.

Since Kotlin

1.0

Samples

import samples.assertPrints
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val answer: Int by lazy {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    42
}

println("What is the answer?")
// Will print 'Computing...' and then 42
println(answer) // 42
println("Come again?")
// Will just print 42
println(answer) // 42 
   //sampleEnd
}

Properties

Link copied to clipboard
abstract val value: T

Gets the lazily initialized value of the current Lazy instance. Once the value was initialized it must not change during the rest of lifetime of this Lazy instance.

Since Kotlin 1.0

Functions

Link copied to clipboard
inline operator fun <T> Lazy<T>.getValue(thisRef: Any?, property: KProperty<*>): T

An extension to delegate a read-only property of type T to an instance of Lazy.

Since Kotlin 1.0
Link copied to clipboard
abstract fun isInitialized(): Boolean

Returns true if a value for this Lazy instance has been already initialized, and false otherwise. Once this function has returned true it stays true for the rest of lifetime of this Lazy instance.

Since Kotlin 1.0