Is there a meaningful difference to what these two...
# getting-started
j
Is there a meaningful difference to what these two things are doing under the hood? Is there a reason to use one over the other (on JVM)?
Copy code
class 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 delegation
y
It compiles to the same exact code, so it's simply a matter of taste: https://godbolt.org/z/re9qsT9EG (there is an extra
delegate
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)