improved lazy delegate; added with(newThis) {} to stdlib
This commit is contained in:
1 parent
f792c73b8f
commit
41a3617850
6 files changed
+85
-8
No files matched your search
@@ -310,6 +310,16 @@ interface Delegate {
|
||||
fun bind(name, access, thisRef) = this
|
||||
}
|
||||
|
||||
/*
|
||||
Executes the block with `this` set to self and
|
||||
returns what the block returns.
|
||||
*/
|
||||
fun with(self, block) {
|
||||
var result = Unset
|
||||
self.apply { result = block() }
|
||||
result
|
||||
}
|
||||
|
||||
/*
|
||||
Standard implementation of a lazy-initialized property delegate.
|
||||
The provided creator lambda is called once on the first access to compute the value.
|
||||
@@ -325,7 +335,7 @@ class lazy(creatorParam) : Delegate {
|
||||
}
|
||||
|
||||
override fun getValue(thisRef, name) {
|
||||
if (value == Unset) value = creator()
|
||||
if (value == Unset) value = with(thisRef,creator)
|
||||
value
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user