Document lazy interface and methods.
This commit is contained in:
@@ -1,17 +1,39 @@
|
|||||||
package kotlin
|
package kotlin
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a value with lazy initialization.
|
||||||
|
*/
|
||||||
public interface Lazy<out T> {
|
public interface Lazy<out T> {
|
||||||
|
/** Gets the lazily initialized value of the current Lazy instance. */
|
||||||
public val value: T
|
public val value: T
|
||||||
|
/** Returns `true` if a value for this Lazy instance has been already initialized. */
|
||||||
public val valueCreated: Boolean
|
public val valueCreated: Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An extension to delegate a read-only property of type [T] to an instance of [Lazy].
|
||||||
|
*
|
||||||
|
* This extension allows to use instances of Lazy for property delegation:
|
||||||
|
* `val property: String by lazy { initializer }`
|
||||||
|
*/
|
||||||
public fun <T> Lazy<T>.get(thisRef: Any?, property: PropertyMetadata): T = value
|
public fun <T> Lazy<T>.get(thisRef: Any?, property: PropertyMetadata): T = value
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies how a [Lazy] instance synchronizes access among multiple threads.
|
||||||
|
*/
|
||||||
public enum class LazyThreadSafetyMode {
|
public enum class LazyThreadSafetyMode {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Locks are used to ensure that only a single thread can initialize the [Lazy] instance.
|
||||||
|
*/
|
||||||
SYNCHRONIZED,
|
SYNCHRONIZED,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* No locks are used to synchronize the access to the [Lazy] instance value; if the instance is accessed from multiple threads, its behavior is undefined.
|
||||||
|
*
|
||||||
|
* This mode should be used only when high performance is crucial and the [Lazy] instance is guaranteed never to be initialized from more than one thread.
|
||||||
|
*/
|
||||||
NONE,
|
NONE,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
package kotlin
|
package kotlin
|
||||||
|
|
||||||
|
|
||||||
|
/** Initializes a new instance of the [Lazy] that uses the specified initialization function [initializer]
|
||||||
|
* and the default thread-safety [LazyThreadSafetyMode.SYNCHRONIZED]. */
|
||||||
public fun lazy<T>(initializer: () -> T): Lazy<T> = LazyImpl(initializer, Any())
|
public fun lazy<T>(initializer: () -> T): Lazy<T> = LazyImpl(initializer, Any())
|
||||||
|
|
||||||
|
/** Initializes a new instance of the [Lazy] that uses the specified initialization function [initializer]
|
||||||
|
* and thread-safety [mode]. */
|
||||||
public fun lazy<T>(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T> =
|
public fun lazy<T>(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T> =
|
||||||
when (mode) {
|
when (mode) {
|
||||||
LazyThreadSafetyMode.SYNCHRONIZED -> LazyImpl(initializer, Any())
|
LazyThreadSafetyMode.SYNCHRONIZED -> LazyImpl(initializer, Any())
|
||||||
|
|||||||
Reference in New Issue
Block a user