diff --git a/libraries/stdlib/src/kotlin/util/Lazy.kt b/libraries/stdlib/src/kotlin/util/Lazy.kt index 7cf7e34815e..48c3fa9afb3 100644 --- a/libraries/stdlib/src/kotlin/util/Lazy.kt +++ b/libraries/stdlib/src/kotlin/util/Lazy.kt @@ -5,12 +5,14 @@ import java.io.Serializable /** * Represents a value with lazy initialization. + * + * To create an instance of [Lazy] use the [lazy] function. */ public interface Lazy { /** Gets the lazily initialized value of the current Lazy instance. */ public val value: T /** Returns `true` if a value for this Lazy instance has been already initialized. */ - public val valueCreated: Boolean + public fun isInitialized(): Boolean } /** @@ -67,9 +69,9 @@ private class LazyImpl(initializer: () -> T) : Lazy, Serializable { } } - override val valueCreated: Boolean get() = _value !== UNINITIALIZED_VALUE + override fun isInitialized(): Boolean = _value !== UNINITIALIZED_VALUE - override fun toString(): String = if (valueCreated) value.toString() else "Lazy value not initialized yet." + override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet." private fun writeReplace(): Any = InitializedLazyImpl(value) } @@ -88,16 +90,16 @@ private class UnsafeLazyImpl(initializer: () -> T) : Lazy, Serializabl return _value as T } - override val valueCreated: Boolean get() = _value !== UNINITIALIZED_VALUE + override fun isInitialized(): Boolean = _value !== UNINITIALIZED_VALUE - override fun toString(): String = if (valueCreated) value.toString() else "Lazy value not initialized yet." + override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet." private fun writeReplace(): Any = InitializedLazyImpl(value) } private class InitializedLazyImpl(override val value: T) : Lazy, Serializable { - override val valueCreated: Boolean get() = true + override fun isInitialized(): Boolean = true override fun toString(): String = value.toString() diff --git a/libraries/stdlib/src/kotlin/util/LazyJVM.kt b/libraries/stdlib/src/kotlin/util/LazyJVM.kt index e49a5d299ec..88b4f74b29c 100644 --- a/libraries/stdlib/src/kotlin/util/LazyJVM.kt +++ b/libraries/stdlib/src/kotlin/util/LazyJVM.kt @@ -5,6 +5,8 @@ package kotlin * Initializes 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. + * * Note that the returned instance uses itself to synchronize on. Do not synchronize from external code on * the returned instance as it may cause accidental deadlock. Also this behavior can be changed in the future. */ @@ -14,6 +16,8 @@ public fun lazy(initializer: () -> T): Lazy = LazyImpl(initializer) * Initializes 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. + * * Note that when the [LazyThreadSafetyMode.SYNCHRONIZED] mode is specified the returned instance uses itself * to synchronize on. Do not synchronize from external code on the returned instance as it may cause accidental deadlock. * Also this behavior can be changed in the future. diff --git a/libraries/stdlib/test/utils/LazyJVMTest.kt b/libraries/stdlib/test/utils/LazyJVMTest.kt index b5bde85cda0..4d89588937c 100644 --- a/libraries/stdlib/test/utils/LazyJVMTest.kt +++ b/libraries/stdlib/test/utils/LazyJVMTest.kt @@ -11,10 +11,10 @@ class LazyJVMTest { test fun lazyInitializationForcedOnSerialization() { for(mode in listOf(LazyThreadSafetyMode.SYNCHRONIZED, LazyThreadSafetyMode.NONE)) { val lazy = lazy(mode) { "initialized" } - assertFalse(lazy.valueCreated) + assertFalse(lazy.isInitialized()) val lazy2 = serializeAndDeserialize(lazy) - assertTrue(lazy.valueCreated) - assertTrue(lazy2.valueCreated) + assertTrue(lazy.isInitialized()) + assertTrue(lazy2.isInitialized()) assertEquals(lazy.value, lazy2.value) } } diff --git a/libraries/stdlib/test/utils/LazyTest.kt b/libraries/stdlib/test/utils/LazyTest.kt index 8e17496ad9b..65943fbf916 100644 --- a/libraries/stdlib/test/utils/LazyTest.kt +++ b/libraries/stdlib/test/utils/LazyTest.kt @@ -22,12 +22,12 @@ class LazyTest { var callCount = 0 val lazyInt = lazy { ++callCount } - assertFalse(lazyInt.valueCreated) + assertFalse(lazyInt.isInitialized()) assertEquals(0, callCount) lazyInt.value - assertTrue(lazyInt.valueCreated) + assertTrue(lazyInt.isInitialized()) }