Fix lazy delegate type resolution across modules
This commit is contained in:
1 parent
217787e17a
commit
7c059b4741
6 files changed
+103
-122
No files matched your search
@@ -415,26 +415,24 @@ fun with<T,R>(self: T, block: T.()->R): R {
|
||||
block(self)
|
||||
}
|
||||
|
||||
extern fun __builtinLazy(creator: Object): Object
|
||||
|
||||
/*
|
||||
Standard implementation of a lazy-initialized property delegate.
|
||||
The provided creator lambda is called once on the first access to compute the value.
|
||||
Can only be used with 'val' properties.
|
||||
*/
|
||||
class lazy<T,ThisRefType=Object>(creatorParam: ThisRefType.()->T) : Delegate<T,ThisRefType> {
|
||||
private val delegate: Delegate<T,ThisRefType> = __builtinLazy(creatorParam) as Delegate<T,ThisRefType>
|
||||
private val creator: ThisRefType.()->T = creatorParam
|
||||
private var value = Unset
|
||||
|
||||
override fun bind(name: String, access: DelegateAccess, thisRef: ThisRefType): Object {
|
||||
delegate.bind(name, access, thisRef)
|
||||
if (access != DelegateAccess.Val) throw "lazy delegate can only be used with 'val'"
|
||||
this
|
||||
}
|
||||
|
||||
override fun getValue(thisRef: ThisRefType, name: String): T {
|
||||
delegate.getValue(thisRef, name) as T
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: ThisRefType, name: String, newValue: T): void {
|
||||
delegate.setValue(thisRef, name, newValue)
|
||||
if (value == Unset)
|
||||
value = with(thisRef, creator)
|
||||
value as T
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user