Android ● ● ○ Kotlin 2.0

Kodebits Day 42: Operator Overloading

Jun 17 2026
Practice operators with a short kotlin challenge.

What does this print?

data class Vec(
  val x: Int, val y: Int
) {
  operator fun plus(v: Vec) =
    Vec(x + v.x, y + v.y)
}
fun main() {
  val a = Vec(1, 2)
  val b = Vec(3, 5)
  println((a + b).y)
}


Try it in the online Kotlin Playground →

[spoiler title="Solution"]

Answer:

7

Explanation:

Operator overloading lets you define + for custom types.

[/spoiler]


Further Reading