Kotlin Academy · Lesson

Index and Invoke

get, set, and invoke.

Lesson 3 of 413 steps

Index and Invoke is a free Kotlin Academy lesson on CoddyKit. This is lesson 3 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.

The Index Operator

Defining operator fun get lets you read with bracket syntax: obj[i].

class Row(val cells: List<String>) {
    operator fun get(i: Int) = cells[i]
}

fun main() {
    val r = Row(listOf("a", "b", "c"))
    println(r[1])
}

Writing with set

operator fun set enables assignment: obj[i] = value. The last parameter is the value.

class Grid {
    private val data = IntArray(3)
    operator fun get(i: Int) = data[i]
    operator fun set(i: Int, value: Int) { data[i] = value }
}

fun main() {
    val g = Grid()
    g[0] = 42
    println(g[0])
}

Multi-Argument Indexing

get and set can take several indices, perfect for matrices.

class Matrix {
    private val d = Array(2) { IntArray(2) }
    operator fun get(r: Int, c: Int) = d[r][c]
    operator fun set(r: Int, c: Int, v: Int) { d[r][c] = v }
}

fun main() {
    val m = Matrix()
    m[1, 1] = 9
    println(m[1, 1])
}

Index with Non-Int Keys

Index parameters can be any type, enabling map-like access.

class Settings {
    private val map = mutableMapOf<String, String>()
    operator fun get(key: String) = map[key]
    operator fun set(key: String, value: String) { map[key] = value }
}

fun main() {
    val s = Settings()
    s["theme"] = "dark"
    println(s["theme"])
}

The Invoke Operator

Defining operator fun invoke lets you call an instance like a function: obj().

class Greeter(val name: String) {
    operator fun invoke() = "Hello, $name"
}

fun main() {
    val g = Greeter("Sam")
    println(g())
}

Invoke with Arguments

invoke can take parameters, making the object behave like a parameterized function.

class Multiplier(val factor: Int) {
    operator fun invoke(x: Int) = x * factor
}

fun main() {
    val triple = Multiplier(3)
    println(triple(5))
}

Stateful Callable Objects

Because the object holds state, invoke can build configurable function-like objects.

class Accumulator {
    var total = 0
    operator fun invoke(n: Int) { total += n }
}

fun main() {
    val acc = Accumulator()
    acc(5)
    acc(10)
    println(acc.total)
}

Multiple Invoke Overloads

You can declare several invoke overloads with different parameters.

class Printer {
    operator fun invoke(s: String) = println(s)
    operator fun invoke(n: Int) = println("num: $n")
}

fun main() {
    val p = Printer()
    p("hi")
    p(7)
}

Index in Expressions

Index access composes naturally with other operations.

class Scores(private val list: List<Int>) {
    operator fun get(i: Int) = list[i]
}

fun main() {
    val s = Scores(listOf(10, 20, 30))
    println(s[0] + s[2])
}

Combining get and invoke

A type can support both indexing and calling for an expressive API.

class Lookup(private val data: Map<String, Int>) {
    operator fun get(key: String) = data[key]
    operator fun invoke() = data.size
}

fun main() {
    val l = Lookup(mapOf("a" to 1))
    println(l["a"])
    println(l())
}

How the Standard Library Uses It

List(rsquo)s list[i] and Map(rsquo)s map[key] are exactly these operators. Your types can feel just as natural.

fun main() {
    val list = listOf(1, 2, 3)
    val map = mapOf("x" to 9)
    println(list[0])
    println(map["x"])
}

Quick Check

Test your understanding of index and invoke.

Recap

You learned index and invoke:

  • get backs obj[i]; set backs obj[i] = v.
  • Index operators support multiple and non-Int keys.
  • invoke lets instances be called like functions, with or without arguments.
  • Standard collections use exactly these operators.
class Box {
    private val m = mutableMapOf<String, Int>()
    operator fun get(k: String) = m[k]
    operator fun set(k: String, v: Int) { m[k] = v }
}

fun main() {
    val b = Box()
    b["x"] = 5
    println(b["x"])
}
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 “Index and Invoke” lesson free?

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

get, set, and invoke. 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 3 of 4.

How long does the “Index and Invoke” 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. Operator Functions
  2. Comparison Operators
  3. Index and Invoke
  4. Best Practices
← Back to Kotlin Academy