Inspecting functional features
Kotlin is a programming language that brings a lot of modern syntactic sugar features to the JVM world. These features increase a developer's efficiency, but they're compiled in the same bytecode as usual Java code. So it's better to know how the internals work so as to prevent unnecessary overhead.
You are already familiar with the Kotlin bytecode inspector, and it will be your main tool in this chapter.
Inline versus non-inline
Let's inspect the following code:
interface ValueHolder<out V> { val value: V } class IntHolder(override val value: Int) : ValueHolder<Int> class DoubleHolder(override val value: Double) : ValueHolder<Double>
The ValueHolder
interface uses a generic to be able to hold a value of any type. The bytecode for the ValueHolder
interface looks like this:
public abstract interface ValueHolder { public abstract getValue()Ljava/lang/Object; LOCALVARIABLE this LValueHolder; L0 L1 0 }
The getValue()
method returns an Object
...