Operator Functions
Overload plus, minus, etc.
Operator Functions is a free Kotlin Academy lesson on CoddyKit. This is lesson 1 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.
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
operatorand 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)
}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 “Operator Functions” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Operator Functions”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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
- Operator Functions
- Comparison Operators
- Index and Invoke
- Best Practices