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
+8 -1
View File
@@ -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) 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) 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) public fun lazy<T>(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T> = UnsafeLazyImpl(initializer)
+5
View File
@@ -15,6 +15,11 @@ public interface Lazy<out T> {
public fun isInitialized(): Boolean 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]. * 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]. * 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. * 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) 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]. * and thread-safety [mode].
* *
* If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access. * 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 } val lazyInt = lazy { ++callCount }
assertEquals(0, callCount) assertEquals(0, callCount)
assertFalse(lazyInt.isInitialized())
assertEquals(1, lazyInt.value) assertEquals(1, lazyInt.value)
assertEquals(1, callCount) assertEquals(1, callCount)
assertTrue(lazyInt.isInitialized())
lazyInt.value lazyInt.value
assertEquals(1, callCount) assertEquals(1, callCount)
} }
test fun valueCreated() { test fun alreadyInitialized() {
var callCount = 0 val lazyInt = lazyOf(1)
val lazyInt = lazy { ++callCount }
assertFalse(lazyInt.isInitialized())
assertEquals(0, callCount)
lazyInt.value
assertTrue(lazyInt.isInitialized()) assertTrue(lazyInt.isInitialized())
assertEquals(1, lazyInt.value)
} }