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]