Extension Functions: Syntax and Dispatch Rules
Write extension functions and understand static dispatch semantics.
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()) // trueAll 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