0Pricing
Kotlin Academy · Lesson

Extension Functions: Syntax and Dispatch Rules

Write extension functions and understand static dispatch semantics.

Extension Functions: Syntax and Dispatch Rules is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Extension Functions?

Extension functions let you add new functions to existing classes without modifying them or using inheritance.

Basic Extension Function Syntax

Prefix the function name with the type you're extending, called the 'receiver type'.
fun String.shout(): String = uppercase() + "!"
println("hello".shout())  // HELLO!
fun Int.isEven() = this % 2 == 0
println(4.isEven())  // true

this in Extensions

Inside an extension function, 'this' refers to the receiver object.
fun List<Int>.secondOrNull(): Int? {
    return if (size >= 2) this[1] else null
}
println(listOf(10, 20, 30).secondOrNull())  // 20
println(listOf(5).secondOrNull())           // null

Static Dispatch: Extensions Don't Override

Extension functions are resolved statically at compile time based on the declared type — they are NOT virtual.
open class Base
class Child : Base()
fun Base.greet() = "Hello from Base"
fun Child.greet() = "Hello from Child"

val obj: Base = Child()
println(obj.greet())  // "Hello from Base" (static dispatch!)

Extensions vs Member Functions

If a class has a member function with the same signature as an extension, the member always wins.
class Greeter {
    fun hello() = "member hello"
}
fun Greeter.hello() = "extension hello"  // shadowed!
println(Greeter().hello())  // "member hello"

Nullable Receiver Extensions

You can extend nullable types. Inside, handle null with 'this == null' checks.
fun String?.orEmpty() = this ?: ""
fun Any?.toStringSafe() = this?.toString() ?: "null"
println(null.orEmpty())  // ""
println(42.toStringSafe())  // "42"

Visibility and Scope

Extensions can be top-level (global), in a class (member extensions), or in a file. They follow normal visibility rules.
// Top-level extension:
fun List<String>.joinUppercase() = joinToString { it.uppercase() }
// Private to file:
private fun String.clean() = trim().lowercase()

Companion Object Extensions

Add extensions to a companion object to extend the class-level namespace.
class User(val name: String) {
    companion object
}
fun User.Companion.guest() = User("Guest")
val guest = User.guest()

Extensions on Standard Library Types

Extend any type — including built-in types.
fun Double.roundTo(decimals: Int): Double {
    val factor = Math.pow(10.0, decimals.toDouble())
    return Math.round(this * factor) / factor
}
println(3.14159.roundTo(2))  // 3.14

Organizing Extensions

Group extensions for a type in a dedicated file, e.g., StringExtensions.kt, DateExtensions.kt.
// StringExtensions.kt
fun String.titleCase() = split(" ")
    .joinToString(" ") {
        it.replaceFirstChar { c -> c.uppercase() }
    }
println("hello world".titleCase())  // Hello World

Extensions Cannot Access Private Members

Extensions only have access to public and internal members — not private or protected ones.
class Secret {
    private val key = "hidden"
    val name = "public"
}
fun Secret.reveal() {
    println(name)  // OK
    // println(key)  // Error: key is private
}

Quick Check

If a class member function and an extension function have the same signature, which one is called?

Recap

Extension functions add behavior to any class without modification. They're statically dispatched (no overriding). Members win over extensions. Use nullable receivers for null-safe helpers. Next: extension properties!

Frequently asked questions

Is the “Extension Functions: Syntax and Dispatch Rules” lesson free?

Yes — the full text of “Extension Functions: Syntax and Dispatch Rules” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Extension Functions: Syntax and Dispatch Rules”?

Write extension functions and understand static dispatch semantics. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Extension Functions: Syntax and Dispatch Rules” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Extension Functions: Syntax and Dispatch Rules
  2. Extension Properties and Computed Extensions
  3. Scoped Extensions and Companion Extensions
  4. Practical Extensions: Context, View & String Helpers
← Back to Kotlin Academy