lazyOf to create already initialized lazy value.

This commit is contained in:
Ilya Gorbunov
2015-06-18 20:21:40 +03:00
parent b71fe4cd25
commit 346ea28337
4 changed files with 20 additions and 11 deletions
+5
View File
@@ -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 -2
View File
@@ -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.
+5 -8
View File
@@ -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)
}