improved lazy delegate; added with(newThis) {} to stdlib

This commit is contained in:
sergeych committed 2026-01-05 22:25:46 +01:00
1 parent f792c73b8f
commit 41a3617850
6 files changed
+85 -8

No files matched your search

+11 -1
View File
@@ -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
}
}