Extension Functions and Properties
Explore Kotlin’s extension functions and properties, gaining insights into their working.
We'll cover the following...
We'll cover the following...
Extension functions
Let’s examine the Java bytecode generated from a Kotlin extension function:
Press + to interact
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:
Press + to interact
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 methodreturn $this$remove.replace(value, "");}public static void main(String[] args) {// parameter not-null checkString var1 = remove("A B C", " ");System.out.println(var1);}}
...