lazyOf to create already initialized lazy value.
This commit is contained in:
@@ -51,6 +51,13 @@ public fun setOf<T>(value: T): Set<T> = hashSetOf(value)
|
||||
*/
|
||||
public fun mapOf<K, V>(keyValuePair: Pair<K, V>): Map<K, V> = hashMapOf(keyValuePair)
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new instance of the [Lazy] that uses the specified initialization function [initializer].
|
||||
*/
|
||||
public fun lazy<T>(initializer: () -> T): Lazy<T> = UnsafeLazyImpl(initializer)
|
||||
|
||||
/**
|
||||
* Creates a new instance of the [Lazy] that uses the specified initialization function [initializer].
|
||||
*
|
||||
* The [mode] parameter is ignored. */
|
||||
public fun lazy<T>(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T> = UnsafeLazyImpl(initializer)
|
||||
|
||||
@@ -15,6 +15,11 @@ public interface Lazy<out T> {
|
||||
public fun isInitialized(): Boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of the [Lazy] that is already initialized with the specified [value].
|
||||
*/
|
||||
public fun lazyOf<T>(value: T): Lazy<T> = InitializedLazyImpl(value)
|
||||
|
||||
/**
|
||||
* An extension to delegate a read-only property of type [T] to an instance of [Lazy].
|
||||
*
|
||||
|
||||
@@ -2,7 +2,7 @@ package kotlin
|
||||
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the [Lazy] that uses the specified initialization function [initializer]
|
||||
* Creates a new instance of the [Lazy] that uses the specified initialization function [initializer]
|
||||
* and the default thread-safety mode [LazyThreadSafetyMode.SYNCHRONIZED].
|
||||
*
|
||||
* If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.
|
||||
@@ -13,7 +13,7 @@ package kotlin
|
||||
public fun lazy<T>(initializer: () -> T): Lazy<T> = LazyImpl(initializer)
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the [Lazy] that uses the specified initialization function [initializer]
|
||||
* Creates a new instance of the [Lazy] that uses the specified initialization function [initializer]
|
||||
* and thread-safety [mode].
|
||||
*
|
||||
* If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.
|
||||
|
||||
@@ -11,23 +11,20 @@ class LazyTest {
|
||||
val lazyInt = lazy { ++callCount }
|
||||
|
||||
assertEquals(0, callCount)
|
||||
assertFalse(lazyInt.isInitialized())
|
||||
assertEquals(1, lazyInt.value)
|
||||
assertEquals(1, callCount)
|
||||
assertTrue(lazyInt.isInitialized())
|
||||
|
||||
lazyInt.value
|
||||
assertEquals(1, callCount)
|
||||
}
|
||||
|
||||
test fun valueCreated() {
|
||||
var callCount = 0
|
||||
val lazyInt = lazy { ++callCount }
|
||||
|
||||
assertFalse(lazyInt.isInitialized())
|
||||
assertEquals(0, callCount)
|
||||
|
||||
lazyInt.value
|
||||
test fun alreadyInitialized() {
|
||||
val lazyInt = lazyOf(1)
|
||||
|
||||
assertTrue(lazyInt.isInitialized())
|
||||
assertEquals(1, lazyInt.value)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user