What is wrong about implementation by delegation?

Under the hood it’s something like this

class X(var a:String): CharSequence {
    private val charSequenceDelegate = a
    override fun iDontKnowTheCharSequnceFunctions() = charSequenceDelegate.iDontKnowTheCharSequnceFunctions()
}

This means that reassigning a will have no effect on the delegate but modifying a value within a directly would.
Ok maybe String is not the best example here since it’s an immutable class but lets just imagine it is something that we can modify.

Also whenever you want to know how a kotlin feature works internally I you can look at the decompiled bytecode. With intellij you can do that under tools>kotlin>show kotlin bytecode.
If you don’t want to or know how to read jvm bytecode the bytecode window has a button to convert the bytecode to java. That way you get an easy to understand representation of the kotlin features. Just a small warning the conversion to java is not perfect. It’s good enough to figure out what happens but you would not be able to compile it with a java compiler.

2 Likes