Move lazy var to separate file
This commit is contained in:
-30
@@ -65,34 +65,4 @@ abstract class IrLazyDeclarationBase(
|
||||
else -> throw AssertionError("Package or class expected: $containingDeclaration; for $currentDescriptor")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal fun <T> lazyVar(initializer: () -> T): UnsafeLazyVar<T> = UnsafeLazyVar(initializer)
|
||||
|
||||
|
||||
internal class UnsafeLazyVar<T>(initializer: () -> T) {
|
||||
private var isInitialized = false;
|
||||
private var initializer: (() -> T)? = initializer
|
||||
private var _value: Any? = null
|
||||
|
||||
private val value: T
|
||||
get() {
|
||||
if (!isInitialized) {
|
||||
_value = initializer!!()
|
||||
isInitialized = true
|
||||
initializer = null
|
||||
}
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return _value as T
|
||||
}
|
||||
|
||||
override fun toString(): String = if (isInitialized) value.toString() else "Lazy value not initialized yet."
|
||||
|
||||
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = value
|
||||
|
||||
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
|
||||
this._value = value
|
||||
isInitialized = true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.jetbrains.kotlin.ir.declarations.lazy
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
internal fun <T> lazyVar(initializer: () -> T): UnsafeLazyVar<T> = UnsafeLazyVar(initializer)
|
||||
|
||||
internal class UnsafeLazyVar<T>(initializer: () -> T) {
|
||||
private var isInitialized = false;
|
||||
private var initializer: (() -> T)? = initializer
|
||||
private var _value: Any? = null
|
||||
|
||||
private val value: T
|
||||
get() {
|
||||
if (!isInitialized) {
|
||||
_value = initializer!!()
|
||||
isInitialized = true
|
||||
initializer = null
|
||||
}
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return _value as T
|
||||
}
|
||||
|
||||
override fun toString(): String = if (isInitialized) value.toString() else "Lazy value not initialized yet."
|
||||
|
||||
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = value
|
||||
|
||||
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
|
||||
this._value = value
|
||||
isInitialized = true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user