Kotlin Academy · Lesson

Extension Properties and Computed Extensions

Add computed properties to existing classes without subclassing.

Lesson 2 of 413 steps

Extension Properties and Computed Extensions is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 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.

Extension Properties

Extension properties add computed properties to existing types without subclassing. They have no backing field.

Read-Only Extension Property

Define a getter to create a computed extension property.
val String.wordCount: Int
    get() = trim().split(Regex("\\s+")).size
println("hello world kotlin".wordCount)  // 3

Mutable Extension Property

Define both getter and setter for a read-write extension property.
var MutableList<Int>.first: Int
    get() = this[0]
    set(value) { this[0] = value }
val list = mutableListOf(1, 2, 3)
list.first = 10
println(list)  // [10, 2, 3]

No Backing Field

Extension properties cannot have backing fields. They're always computed from existing state.
// ERROR: extension property cannot have backing field
// var String.cache: String = ""  // NOT allowed!

// OK: computed only:
val String.isPalindrome: Boolean
    get() = this == this.reversed()

Extension Properties on Collections

Add convenient properties to collection types.
val <T> List<T>.secondOrNull: T?
    get() = if (size >= 2) this[1] else null
val <T> List<T>.isNotNullOrEmpty: Boolean
    get() = isNotEmpty()
println(listOf(1, 2, 3).secondOrNull)  // 2

Boolean Extension Properties

Create readable Boolean properties for type checks.
val Any.isString: Boolean get() = this is String
val String.hasDigits: Boolean get() = any { it.isDigit() }
val Int.isPositive: Boolean get() = this > 0
println(42.isPositive)        // true
println("abc123".hasDigits)   // true

Extension Properties vs Extension Functions

Use a property when the computation is simple and represents a characteristic of the object. Use a function when it has parameters or side effects.
// Property: stateless, simple characteristic
val String.isEmail: Boolean get() = '@' in this
// Function: has parameters
fun String.padCenter(width: Int) = padStart((length + width) / 2)

Extending a Companion Object

Add extension properties to companion objects.
data class Color(val r: Int, val g: Int, val b: Int) {
    companion object
}
val Color.Companion.RED get() = Color(255, 0, 0)
val Color.Companion.WHITE get() = Color(255, 255, 255)
println(Color.RED)  // Color(r=255, g=0, b=0)

Contextual Extension Properties

Extension properties defined inside a class only apply when you're inside that class's scope.
class Formatter {
    val Int.formatted: String
        get() = "%,d".format(this)
    fun format(n: Int) = n.formatted
}
println(Formatter().format(1000000))  // 1,000,000

Using Extensions for DSLs

Extension properties are fundamental to Kotlin DSL design.
val Int.dp: Float get() = this * 2.5f
val Float.sp: Float get() = this * 1.0f
// In DSL context:
view.setPadding(16.dp.toInt())

Generic Extension Properties

Extension properties can have type parameters.
val <T> List<T>.head: T?
    get() = firstOrNull()
val <T> List<T>.tail: List<T>
    get() = drop(1)
println(listOf(1,2,3).head)  // 1
println(listOf(1,2,3).tail)  // [2, 3]

Quick Check

Can extension properties have backing fields in Kotlin?

Recap

Extension properties add computed characteristics to types without backing fields. Use them for simple, stateless computations. Mutable extension properties can have setters. Great for DSLs and fluent APIs. Next: scoped and companion extensions!
Free to start

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 Properties and Computed Extensions” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Extension Properties and Computed Extensions”, 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 Properties and Computed Extensions”?

Add computed properties to existing classes without subclassing. 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 2 of 4.

How long does the “Extension Properties and Computed Extensions” 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

  1. Extension Functions: Syntax and Dispatch Rules
  2. Extension Properties and Computed Extensions
  3. Scoped Extensions and Companion Extensions
  4. Practical Extensions: Context, View & String Helpers
← Back to Kotlin Academy