Index and Invoke
get, set, and invoke.
Index and Invoke is a free Kotlin Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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:
getbacksobj[i];setbacksobj[i] = v.- Index operators support multiple and non-Int keys.
invokelets 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"])
}Frequently asked questions
Is the “Index and Invoke” lesson free?
Yes — the full text of “Index and Invoke” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
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
- Operator Functions
- Comparison Operators
- Index and Invoke
- Best Practices