diff --git a/js/js.libraries/src/core/kotlin.kt b/js/js.libraries/src/core/kotlin.kt index b3445718a37..219612926b3 100644 --- a/js/js.libraries/src/core/kotlin.kt +++ b/js/js.libraries/src/core/kotlin.kt @@ -51,6 +51,13 @@ public fun setOf(value: T): Set = hashSetOf(value) */ public fun mapOf(keyValuePair: Pair): Map = hashMapOf(keyValuePair) - +/** + * Creates a new instance of the [Lazy] that uses the specified initialization function [initializer]. + */ public fun lazy(initializer: () -> T): Lazy = UnsafeLazyImpl(initializer) + +/** + * Creates a new instance of the [Lazy] that uses the specified initialization function [initializer]. + * + * The [mode] parameter is ignored. */ public fun lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy = UnsafeLazyImpl(initializer) diff --git a/libraries/stdlib/src/kotlin/util/Lazy.kt b/libraries/stdlib/src/kotlin/util/Lazy.kt index 48c3fa9afb3..73dda4356e4 100644 --- a/libraries/stdlib/src/kotlin/util/Lazy.kt +++ b/libraries/stdlib/src/kotlin/util/Lazy.kt @@ -15,6 +15,11 @@ public interface Lazy { public fun isInitialized(): Boolean } +/** + * Creates a new instance of the [Lazy] that is already initialized with the specified [value]. + */ +public fun lazyOf(value: T): Lazy = InitializedLazyImpl(value) + /** * An extension to delegate a read-only property of type [T] to an instance of [Lazy]. * diff --git a/libraries/stdlib/src/kotlin/util/LazyJVM.kt b/libraries/stdlib/src/kotlin/util/LazyJVM.kt index 88b4f74b29c..804590a0699 100644 --- a/libraries/stdlib/src/kotlin/util/LazyJVM.kt +++ b/libraries/stdlib/src/kotlin/util/LazyJVM.kt @@ -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(initializer: () -> T): Lazy = 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. diff --git a/libraries/stdlib/test/utils/LazyTest.kt b/libraries/stdlib/test/utils/LazyTest.kt index 65943fbf916..1e2ff510c56 100644 --- a/libraries/stdlib/test/utils/LazyTest.kt +++ b/libraries/stdlib/test/utils/LazyTest.kt @@ -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) }