Search⌘ K

Extension Functions and Properties

Explore Kotlin’s extension functions and properties, gaining insights into their working.

We'll cover the following...

Extension functions

Let’s examine the Java bytecode generated from a Kotlin extension function:

Kotlin 1.5
fun String.remove(value: String) = this.replace(value, "")
fun main() {
println("A B C".remove(" ")) // ABC
}

As a result, we should see the following code:

Java
final class PlaygroundKt {
public static final String remove(
String $this$remove,
String value
) {
// Null-check using a conditional operator
$this$remove = ($this$remove != null) ? $this$remove : "";
value = (value != null) ? value : "";
// Use standard Java replace method
return $this$remove.replace(value, "");
}
public static void main(String[] args) {
// parameter not-null check
String var1 = remove("A B C", " ");
System.out.println(var1);
}
}
...