Skip to content
Justice Tech School

Operator Overloading

Mark a function operator to give + - * and others meaning for your own types, for example operator fun plus.

Example
data class V(val x: Int, val y: Int) { operator fun plus(o: V) = V(x + o.x, y + o.y) }
println(V(1, 2) + V(3, 4))

Exercise

Overload plus.

operator fun (o: V)

Quiz Test

Which keyword enables overloading?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.