0Pricing
Kotlin Academy · Lesson

Operator Functions

Overload plus, minus, etc.

Operator Functions is a free Kotlin Academy lesson on CoddyKit — lesson 1 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.

What Is Operator Overloading?

Operator overloading lets your types respond to symbols like + or -. You define a function with the operator modifier and a special name.

data class Point(val x: Int, val y: Int) {
    operator fun plus(o: Point) = Point(x + o.x, y + o.y)
}

fun main() {
    println(Point(1, 2) + Point(3, 4))
}

The operator Keyword

The operator modifier tells the compiler this function backs an operator. Without it, calling a + b would not work.

data class Money(val amount: Int) {
    operator fun plus(o: Money) = Money(amount + o.amount)
}

fun main() {
    println(Money(5) + Money(3))
}

Conventional Names

Each operator maps to a fixed function name: plus for +, minus for -, times for *, div for /, rem for %.

data class Vec(val n: Int) {
    operator fun times(k: Int) = Vec(n * k)
}

fun main() {
    println(Vec(4) * 3)
}

Minus and Times

You can define several operators on the same type.

data class Point(val x: Int, val y: Int) {
    operator fun minus(o: Point) = Point(x - o.x, y - o.y)
    operator fun times(k: Int) = Point(x * k, y * k)
}

fun main() {
    println(Point(5, 5) - Point(1, 2))
    println(Point(2, 3) * 2)
}

Unary Operators

Unary minus uses unaryMinus; unary plus uses unaryPlus; not backs the ! operator.

data class Temp(val c: Int) {
    operator fun unaryMinus() = Temp(-c)
}

fun main() {
    println(-Temp(10))
}

Increment and Decrement

inc backs ++ and dec backs --. They return a new value of the same type.

data class Counter(val n: Int) {
    operator fun inc() = Counter(n + 1)
}

fun main() {
    var c = Counter(0)
    c++
    println(c.n)
}

Compound Assignment

plusAssign backs += for mutable receivers. If only plus exists, += reassigns the variable instead.

class Bag {
    val items = mutableListOf<String>()
    operator fun plusAssign(item: String) { items.add(item) }
}

fun main() {
    val bag = Bag()
    bag += "apple"
    println(bag.items)
}

Operators with Different Types

The parameter type need not match the receiver. Here scaling a point by an Int returns a point.

data class Size(val w: Int, val h: Int) {
    operator fun times(factor: Int) = Size(w * factor, h * factor)
}

fun main() {
    println(Size(2, 3) * 4)
}

As Extension Functions

Operators can be defined as extensions, even for types you do not own.

data class Point(val x: Int, val y: Int)

operator fun Point.plus(o: Point) = Point(x + o.x, y + o.y)

fun main() {
    println(Point(1, 1) + Point(2, 2))
}

Chaining Operators

Overloaded operators compose like built-in ones, respecting normal precedence.

data class N(val v: Int) {
    operator fun plus(o: N) = N(v + o.v)
    operator fun times(o: N) = N(v * o.v)
}

fun main() {
    val r = N(2) + N(3) * N(4)
    println(r.v)
}

Return Types Are Free

An operator can return any type, but returning the same type keeps usage intuitive and chainable.

data class Vec(val x: Int, val y: Int) {
    operator fun plus(o: Vec) = Vec(x + o.x, y + o.y)
}

fun main() {
    println(Vec(1, 2) + Vec(3, 4))
}

Quick Check

Test your understanding of operator functions.

Recap

You learned operator functions:

  • Mark functions with operator and a conventional name (plus, minus, times, ...).
  • Unary, increment, and compound-assignment operators are supported.
  • Operators can be members or extensions.
  • Returning the same type keeps chaining intuitive.
data class P(val x: Int) {
    operator fun plus(o: P) = P(x + o.x)
}

fun main() {
    println((P(2) + P(3)).x)
}

Frequently asked questions

Is the “Operator Functions” lesson free?

Yes — the full text of “Operator Functions” 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 “Operator Functions”?

Overload plus, minus, etc. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Operator Functions” 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