PART- 1 How does extension function internally work in Kotlin??

Sandeep Kella
3 min readJun 13, 2024

In Kotlin, extension functions are a powerful feature that allows you to add new functionality to existing classes without modifying their source code. Under the hood, Kotlin extension functions are just static methods that take the receiver object as their first parameter. Let’s dive into how extension functions work internally and understand their implementation.

How Extension Functions Work

  1. Definition:
  • You define an extension function by prefixing the function name with the receiver type.
fun String.addExclamation(): String {
return this + "!"
}

2. Usage:

  • You can call the extension function on any instance of the receiver type as if it were a member function.
val message = "Hello"
println(message.addExclamation()) // Outputs: Hello!

Internal Implementation

When you define and use an extension function, Kotlin internally generates a static method with the receiver object as its first parameter. Let’s see what happens step by step:

a. Extension Function Definition:

// Extension function on the String class
fun String.addExclamation(): String {
return this + "!"
}

b. Usage of Extension Function:

val message = "Hello"
println(message.addExclamation()) // Outputs: Hello!

c. Internal Representation:

Internally, the extension function is compiled into a static method. Here’s a rough equivalent in Java:

public final class ExtensionFunctionsKt {
public static String addExclamation(String receiver) {
return receiver + "!";
}
}

d. Calling the Extension Function:

When you call message.addExclamation(), Kotlin translates it into:

String message = "Hello";
String result = ExtensionFunctionsKt.addExclamation(message);
System.out.println(result); // Outputs: Hello!

Key Points

  1. No Changes to the Receiver Class:
  • Extension functions do not actually modify the classes they extend. They simply provide a way to call new methods as if they were part of the class.

2. Static Methods:

  • Extension functions are compiled into static methods with the receiver object passed as the first parameter.

3. Scope and Visibility:

  • Extension functions respect visibility and scope. If you define an extension function within a certain scope, it will only be available within that scope.

Example with Scope and Visibility

// File: Utils.kt
package com.example.utils

fun String.addExclamation(): String {
return this + "!"
}

// File: Main.kt
package com.example.main

import com.example.utils.addExclamation

fun main() {
val message = "Hello"
println(message.addExclamation()) // Outputs: Hello!
}

In this example, addExclamation is defined in the com.example.utils package and is imported in the com.example.main package.

Extension Functions with Nullable Receiver

You can also define extension functions for nullable receivers. These functions can be called on null objects.

fun String?.isNullOrEmpty(): Boolean {
return this == null || this.isEmpty()
}

fun main() {
val message: String? = null
println(message.isNullOrEmpty()) // Outputs: true
}

Internally, the nullable receiver is treated just like any other nullable parameter in Java.

Summary

  • Extension functions in Kotlin are syntactic sugar for static methods that take the receiver object as their first parameter.
  • They do not modify the original class but provide additional functionality as if it were a member function.
  • The compiler generates static methods that handle the receiver as a parameter, which makes calling extension functions equivalent to calling static methods.
  • Extension functions respect scope and visibility and can be defined for nullable types as well.

This allows you to extend existing classes with new functionalities in a clean and maintainable way without altering their source code or using inheritance.

--

--

Sandeep Kella

Android developer @PhonePe, writes about Android development and productivity.