🐦 Kotlin Beginner

What is an extension function in Kotlin?

Answer

An extension function adds a new method to an existing class without modifying the class itself, inheriting from it, or using decorators. The syntax is fun ClassName.newMethod(): ReturnType { }. Inside the function, this refers to the object the function is called on. Example: fun String.isPalindrome() = this == this.reversed() — now "racecar".isPalindrome() returns true. Extensions are resolved statically (at compile time), not polymorphically. They are a key Kotlin feature for creating readable, fluent APIs without inheritance.