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. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
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
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Extension Functions: Syntax and Dispatch Rules” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Extension Functions: Syntax and Dispatch Rules”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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