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?
Basic Extension Function Syntax
fun String.shout(): String = uppercase() + "!"
println("hello".shout()) // HELLO!
fun Int.isEven() = this % 2 == 0
println(4.isEven()) // truethis in Extensions
fun List<Int>.secondOrNull(): Int? {
return if (size >= 2) this[1] else null
}
println(listOf(10, 20, 30).secondOrNull()) // 20
println(listOf(5).secondOrNull()) // nullStatic Dispatch: Extensions Don't Override
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
class Greeter {
fun hello() = "member hello"
}
fun Greeter.hello() = "extension hello" // shadowed!
println(Greeter().hello()) // "member hello"Nullable Receiver Extensions
fun String?.orEmpty() = this ?: ""
fun Any?.toStringSafe() = this?.toString() ?: "null"
println(null.orEmpty()) // ""
println(42.toStringSafe()) // "42"Visibility and Scope
// Top-level extension:
fun List<String>.joinUppercase() = joinToString { it.uppercase() }
// Private to file:
private fun String.clean() = trim().lowercase()Companion Object Extensions
class User(val name: String) {
companion object
}
fun User.Companion.guest() = User("Guest")
val guest = User.guest()Extensions on Standard Library 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.14Organizing Extensions
// StringExtensions.kt
fun String.titleCase() = split(" ")
.joinToString(" ") {
it.replaceFirstChar { c -> c.uppercase() }
}
println("hello world".titleCase()) // Hello WorldExtensions Cannot Access Private Members
class Secret {
private val key = "hidden"
val name = "public"
}
fun Secret.reveal() {
println(name) // OK
// println(key) // Error: key is private
}Quick Check
Recap
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
- Extension Functions: Syntax and Dispatch Rules
- Extension Properties and Computed Extensions
- Scoped Extensions and Companion Extensions
- Practical Extensions: Context, View & String Helpers