CodeGym /Courses /Kotlin SELF /Extension properties

Extension properties

Kotlin SELF
Level 38, Lesson 1
Available

1. Introduction

When you write a learning project (for example, our console expense tracker), the code quickly grows lots of small “conveniences”: normalize a command here, make a nice label for an amount there, compute a “last index of a string, but safely”. All of this can be done with functions — and that’s fine. But sometimes, semantically, you want exactly a property, because we are not “doing an action”, we are “looking at a characteristic” of an object: expense.title, money.formatted, command.normalized.

Extension properties help make code more “readable to the eye”. They are especially nice in places where you format output, build reports, do simple checks and transformations. The important thing is to remember that this is not magic and not “adding fields to an object”: Kotlin does not change the original classes; we just get more convenient access syntax.

2. How extension properties work: why this is not a field

An extension property looks like a property, but under the hood it is just a pair of accessor functions (getter/setter). So it has no “place inside the object” where you can put a value. Kotlin is straightforward: if you want an extension property — sure, but only via get()/set(). And that is why you cannot write = 123 as an “initialization” — the compiler forbids it.

The basic forms look like this:

val String.normalized: String
    get() = trim().lowercase()

var StringBuilder.indent: Int
    get() = 0 // example, but meaningless
    set(value) { /* ... */ }

And a useful “mental model” (very rough, but it works for understanding):

obj.someProp
   ↓
getSomeProp(obj)  // conceptually: generated getter function

You can even draw a mini diagram:

flowchart LR
    A["obj.someProp"] --> B["someProp.get()"]
    B --> C["get() code runs"]
    C --> D["value is returned"]

To avoid confusion, keep a simple rule in mind: if you can think of it as “a computed value right now” — an extension property fits. If you need to “remember a value in the object and then read it later” — an extension property does not fit.

Below is a small comparison table to reinforce the difference between a regular class property and an extension property:

What we compare Regular property in a class Extension property
Is there a backing field (internal storage field) May exist (field) No
Can you write = ... as initialization Yes No, forbidden
Can you make a computed property Yes Yes
Can you store state “in the object” Yes No (only external tricks)
Access to the class’s private fields Yes No

3. Examples: strings and data models

Normalizing command input

In console apps, half the “pain” is user input. The user types " ADD ", "Add", "add", sometimes even "aDd" (why love them for that?). So normalizing a string is an evergreen thing.

In previous lectures we would have done this with a function. But here you can afford a property, because “a normalized version of a string” is a characteristic of the string, not an “action that changes something”.

val String.normalizedCommand: String
    get() = trim().lowercase()

fun main() {
    val raw = "  AdD   "
    println(raw.normalizedCommand) // add
}

This reads almost like “the string has a normalized command”. But remember: the string does not change — we just compute the result each time.

Another classic example is “last index, but safely”. For an empty string length == 0, and length - 1 gives -1. That’s fine if that’s what you intended (for example, so you don’t touch indexes at all).

val String.lastIndexOrMinusOne: Int
    get() = length - 1

fun main() {
    println("".lastIndexOrMinusOne)       // -1
    println("kotlin".lastIndexOrMinusOne) // 5
}

Here the extension property is basically “documentation in code”: you see the name lastIndexOrMinusOne and immediately understand what happens on an empty string.

Formatting Money and output strings for Expense

When a project grows, you almost inevitably introduce your own types. In an expense tracker it is convenient to have a “money” type so you don’t confuse “francs/dollars” with “internal kopeks/cents”, and you don’t accidentally add strings to numbers.

Let’s make a minimal money type via value class (we have already seen them in the type-safety section), and move formatting into an extension property. Important: formatting is “presentation”; it does not change the object, so a property is appropriate here.

@JvmInline
value class Money(val cents: Long)

val Money.formatted: String
    get() {
        val sign = if (cents < 0) "-" else ""
        val abs = kotlin.math.abs(cents)
        val dollars = abs / 100
        val rest = abs % 100
        return "$sign$dollars.${rest.toString().padStart(2, '0')}"
    }

fun main() {
    println(Money(12345).formatted)  // 123.45
    println(Money(-90).formatted)    // -0.90
}

Yes, this computation is not “free” (there is abs and padStart). But it is light enough and expected: when you write money.formatted, you are essentially asking for a “string representation”. That’s fine.

Now let’s add an expense model and make a nice line for a list. Again: we don’t want to clutter a data class with formatters (often you want to keep a data model simple), so an extension property is a good compromise.

data class Expense(
    val title: String,
    val amount: Money,
    val category: String
)

val Expense.listLine: String
    get() = "${category.padEnd(10)} | ${title.padEnd(15)} | ${amount.formatted}"

fun main() {
    val e = Expense("Coffee", Money(390), "food")
    println(e.listLine) // food       | Coffee          | 3.90
}

Notice how “honest” the name is. listLine says directly: this is a line for output. If we named it val Expense.info, it would be less clear which format it is and where it is used.

4. var extension properties and external storage

With val extension properties, everything is usually fine: they compute and return. But var raises a natural question: “If I implement set(value), where is it written?” And this is where you should stop and mentally ask: “Am I sure I’m not trying to attach a field to the object?”

Kotlin allows var extension properties, but it reminds you: they have no backing field, so by themselves they store nothing. If you need to “attach a value as if it were a field”, you will have to store it somewhere outside (usually in a Map).

Let’s show this with an example very similar to the one in the Kotlin documentation. Suppose we want to add a “tag” to an expense (for example, "coffee", "home", "urgent"), but we don’t want to (or can’t) change Expense right now.

We’ll make external storage and an extension property that looks into it. To make the example more stable, we’ll key it by id, not by the object instance. (Note: for the example, a simplified version of Expense is used.)

data class Expense(
    val id: Int,
    val title: String
)

private val tagsById = mutableMapOf<Int, String>()

var Expense.tag: String?
    get() = tagsById[id]
    set(value) {
        if (value == null) tagsById.remove(id) else tagsById[id] = value
    }

fun main() {
    val e = Expense(id = 1, title = "Coffee")

    println(e.tag) // null
    e.tag = "drink"
    println(e.tag) // drink
}

Technically, it works. But design-wise this is already “thin ice”: Expense looks as if it has a tag field, but in reality the tag lives in a separate map. This can be an acceptable temporary solution, but it is easy to turn it into a source of weird bugs (for example, if id is reused, or if you forget to clear tagsById).

5. Limitations: initialization, heavy get(), and side effects

Why you cannot initialize an extension property

Sometimes a beginner thinks: “Alright, since there is no field, I’ll create it now. I’ll write var X.y = 0 and done.” Kotlin answers: “Initializers for extension properties are forbidden.” The reason is simple: an initializer implies storing a value, but there is nowhere to store it — we cannot physically add a field to someone else’s object.

Here is the “wrong dream” that won’t compile:

data class User(val name: String)

var User.age = 0 // will NOT compile: no get/set and no field

If you really want User to have an age field, the correct options are usually: add the property to the class itself, make a wrapper around the object, or honestly store state in external storage (for example, in a repository/map) without pretending it is “a field inside”.

And yes, you can hide external storage behind a var extension property (as in the example above). But that’s a conscious trick, not the “normal way to add a field”.

Heavy get() and unexpected effects

There is a subtle psychological thing: when you see obj.prop, your brain expects it to be fast and surprise-free. And when you see obj.calculateTotal(), your brain is already prepared that there may be computation. So a property with a heavy get() can become a trap for the reader — and for you a month later.

Let’s show this with an example of a list of expenses. It feels tempting to write this:

@JvmInline
value class Money(val cents: Long)

data class Expense(val amount: Money)

val List<Expense>.total: Money
    get() {
        var sum = 0L
        for (e in this) sum += e.amount.cents
        return Money(sum)
    }

Formally, this is correct. But now expenses.total is a full pass over the entire list every time. If you call this property ten times in a loop, you will do ten passes. For a learning project with small data it’s survivable, but the habit is dangerous.

In such cases it is often better to choose an extension function (which we already know how to write), so the call name more honestly warns about computation. Just because Kotlin allows a property does not mean it is always better.

Even worse is when get() performs side effects. For example, prints or mutates a collection. This turns obj.prop into a “tiny horror story”: you merely read a value, and the application suddenly changed something or printed to the console.

6. Where to keep extension properties in a project

When extensions become useful, there is a temptation to create a file Extensions.kt and dump everything there: strings, money, reports, kittens. That quickly turns into a graveyard of random functions where it is impossible to find anything (like the Downloads folder, only worse).

Kotlin conventions suggest a pretty sensible idea: extension functions/properties that matter “to all clients” of a class are best kept near the class. And those that are needed for a specific scenario (for example, only for CLI output) should be kept near that scenario, not globally.

For our learning expense tracker, you can think of it like this: the Expense.listLine format is specifically console output, so it makes sense to keep it near the CLI code or in a formatting file. But Money.formatted, if it is used in different places, makes sense to keep near Money or in a “financial” module of the project.

And one more practical point: extension properties are brought in via imports, and naming conflicts are very real. The more precise and domain-specific names you choose (listLine, formatted, normalizedCommand), the less likely you are to accidentally create two val String.value in different packages and then play “guess the import”.

7. Common mistakes when working with extension properties

Mistake #1: trying to “bolt on a field” via var X.prop = ....
This is a common reflex after languages where dynamic fields or metaprogramming exist. Kotlin doesn’t work like that: an extension property cannot have an initializer, because that implies storing a value in the object, and we are not changing the object. The correct strategy is to write get/set and understand where the state will actually live.

Mistake #2: a “heavy” get() disguised as a simple property.
If get() does noticeable work (iterates a large collection, builds a long string, reads a file), the reader of the code may not expect that cost. As a result you get unexpected slowdowns and duplicated computations. In such cases it is usually more honest to choose a function so the syntax itself hints “there is computation here”.

Mistake #3: side effects in get(): printing, mutation, hidden changes.
Properties in Kotlin read like “took a value”. If your get() does println() inside, changes a global variable, or modifies a collection, the code becomes unpredictable. It’s better to model such things as functions with descriptive names so the action is explicit.

Mistake #4: var extension property that “seems to store”, but actually stores in some unclear place.
The MutableMap trick is sometimes needed (as a temporary adapter), and Kotlin even shows a similar example in the documentation. But if it is not obvious to the code reader where the value lives and who owns the lifecycle of that storage, you get hard-to-debug issues. If the property should semantically be part of the model — most often it is better to add a normal property to the class.

Mistake #5: expecting access to private class fields from an extension property.
An extension property does not become “part of the class” and does not gain privileges. It can only see what is visible from the outside. If you hit private, that’s a signal: either you need a public method/property in the class itself, or you’re trying to break encapsulation and should rethink the design.

1
Task
Kotlin SELF, level 38, lesson 1
Locked
Terminal command
Terminal command
1
Task
Kotlin SELF, level 38, lesson 1
Locked
Last character
Last character
1
Task
Kotlin SELF, level 38, lesson 1
Locked
Checkout price tag
Checkout price tag
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION