From 298958658273051ccd34b6d158fae25d205f50e5 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 22 Jun 2015 21:34:50 +0300 Subject: [PATCH] Change Lazy from an interface to an abstract class with internal constructor to prevent implementations violating its contract. --- libraries/stdlib/src/kotlin/util/Lazy.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/libraries/stdlib/src/kotlin/util/Lazy.kt b/libraries/stdlib/src/kotlin/util/Lazy.kt index 73dda4356e4..b155ed2bb54 100644 --- a/libraries/stdlib/src/kotlin/util/Lazy.kt +++ b/libraries/stdlib/src/kotlin/util/Lazy.kt @@ -8,11 +8,13 @@ import java.io.Serializable * * To create an instance of [Lazy] use the [lazy] function. */ -public interface Lazy { +public abstract class Lazy internal constructor() { /** 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 fun isInitialized(): Boolean + public abstract val value: T + /** Returns `true` if a value for this Lazy instance has been already initialized, and `false` otherwise. + * Once this function has returned `true` it stays `true` for the rest of lifetime of this Lazy instance. + */ + public abstract fun isInitialized(): Boolean } /** @@ -49,7 +51,7 @@ public enum class LazyThreadSafetyMode { private object UNINITIALIZED_VALUE -private class LazyImpl(initializer: () -> T) : Lazy, Serializable { +private class LazyImpl(initializer: () -> T) : Lazy(), Serializable { private var initializer: (() -> T)? = initializer private volatile var _value: Any? = UNINITIALIZED_VALUE @@ -82,7 +84,7 @@ private class LazyImpl(initializer: () -> T) : Lazy, Serializable { } -private class UnsafeLazyImpl(initializer: () -> T) : Lazy, Serializable { +private class UnsafeLazyImpl(initializer: () -> T) : Lazy(), Serializable { private var initializer: (() -> T)? = initializer private var _value: Any? = UNINITIALIZED_VALUE @@ -102,7 +104,7 @@ private class UnsafeLazyImpl(initializer: () -> T) : Lazy, Serializabl private fun writeReplace(): Any = InitializedLazyImpl(value) } -private class InitializedLazyImpl(override val value: T) : Lazy, Serializable { +private class InitializedLazyImpl(override val value: T) : Lazy(), Serializable { override fun isInitialized(): Boolean = true