100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Kotlin Extension Functions Cheat Sheet

Kotlin Extension Functions Cheat Sheet

Covers defining extension functions and properties, receiver types, extension resolution rules, and common standard library extension idioms in Kotlin.

1 PageIntermediateMar 22, 2026

Basic Extension Function

Adding a function to an existing type without subclassing it.

kotlin
// Adds a function to String without modifying its sourcefun String.isPalindrome(): Boolean {    val cleaned = this.lowercase().filter { it.isLetterOrDigit() }    return cleaned == cleaned.reversed()}println("Racecar".isPalindrome()) // true// Extension with a parameterfun Int.times(action: () -> Unit) {    repeat(this) { action() }}3.times { println("Hi") }

Extension Properties

Computed properties added to an existing type.

kotlin
// Extension properties cannot have backing fields; must be computedval String.lastChar: Char    get() = this[length - 1]println("Kotlin".lastChar) // 'n'val <T> List<T>.secondOrNull: T?    get() = if (size >= 2) this[1] else nullprintln(listOf(1, 2, 3).secondOrNull) // 2

Resolution Rules

How Kotlin decides which extension function to call.

  • Statically resolved- Extension functions are resolved at compile time based on the declared (static) type
  • Member functions win- If a class has a member with the same signature, the member always takes priority
  • No true polymorphism- Calling an extension on a variable typed as the base class uses the base class's extension, not the runtime type's
  • Nullable receiver allowed- `fun Any?.safePrint()` can be called on a null reference and check `this == null` inside
  • Import required- Extensions from another package must be imported (or in scope) to be visible

Scope Functions

let, run, with, apply, and also — extension functions in the stdlib.

kotlin
val result = "hello".let {    println("Length: ${it.length}")    it.uppercase() // returned as result}data class Person(var name: String, var age: Int)val person = Person("Ana", 30).apply {    age = 31 // `this` refers to the receiver; returns the receiver itself}val message = with(person) {    "$name is $age years old" // `this` is implicit inside with}person.also {    println("Created: $it") // returns the receiver, for side effects/chaining}

Common stdlib Extensions

Widely used extension functions from the Kotlin standard library.

  • String.trim() / isBlank() / isNotEmpty()- Common string extension helpers in kotlin.text
  • Iterable<T>.map/filter/fold- Higher-order extension functions on collections
  • Any?.let { }- Runs a block with the receiver as `it`, useful for null-checked chains
  • Int.coerceIn(min, max)- Clamps a numeric value within a range
  • File.readText()- kotlin.io extension for reading a file's contents as a String
  • Collection<T>.firstOrNull { }- Returns the first matching element or null instead of throwing
Pro Tip

Because extension resolution is static, never rely on an extension function to behave polymorphically across a class hierarchy — if you need runtime-type-dependent behavior, use a real member function or override, not an extension.

Was this cheat sheet helpful?

Explore Topics

#KotlinExtensionFunctions#KotlinExtensionFunctionsCheatSheet#Programming#Intermediate#BasicExtensionFunction#ExtensionProperties#ResolutionRules#ScopeFunctions#Functions#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet