Joshua Hansen
03/05/2025, 9:33 PMclass Foo {
private val bar: List<Int> = emptyList()
val barSize1: Int by bar::size
val barSize2: Int get() = bar.size
}
I've seen #2 used in code (Even official Kotlin kotlin) and wondered why it is used over delegationYoussef Shoaib [MOD]
03/05/2025, 9:49 PMdelegate
static function created, but like, any minifier worth their salt will just see that that's unused and remove it).
IIRC, this pattern is optimized in the compiler. Some of these optimizations are kind-of ad-hoc right now, but that might be cleaned up if this issue ever gets implemented (at least I hope it would streamline this more and show you easily that this is guaranteed to be optimized)