What if the plus sign you use every day was actually a secret function you could customize?
Why operators are functions in Kotlin - The Real Reasons
Imagine you want to add two custom objects, like two points on a map, by writing code that just adds them with a plus sign.
Without operators as functions, you must write separate methods and call them explicitly, making your code long and hard to read.
Manually calling methods like point1.add(point2) everywhere is slow to write and clutters your code.
It's easy to make mistakes or forget to call the right method, and your code looks less like natural math.
Kotlin treats operators like + as special functions you can define for your own types.
This means you can write point1 + point2 and Kotlin calls your function behind the scenes, making code clean and intuitive.
val result = point1.add(point2)
val result = point1 + point2
This lets you write code that reads like natural math or language, making it easier to understand and maintain.
For example, in a game, you can add two vectors with vector1 + vector2 instead of calling a method, making your game logic clearer and simpler.
Operators in Kotlin are actually functions you can define.
This makes code shorter, clearer, and less error-prone.
You can use natural symbols like + with your own types.
Practice
Solution
Step 1: Understand Kotlin's operator design
Kotlin treats operators as functions so you can define how they behave for your own classes.Step 2: Recognize the benefit
This makes code flexible and readable by allowing operator overloading.Final Answer:
To allow customizing operator behavior for different types -> Option AQuick Check:
Operators as functions = Custom behavior [OK]
- Thinking operators are only for math
- Believing Kotlin lacks functions
- Assuming operators make code faster
Solution
Step 1: Recall Kotlin operator function syntax
Operator functions must use the keywordoperatorbeforefun.Step 2: Check the options
Only operator fun plus(other: MyClass): MyClass correctly placesoperatorbeforefunand defines the function properly.Final Answer:
operator fun plus(other: MyClass): MyClass -> Option AQuick Check:
operator keyword before fun = correct syntax [OK]
- Placing 'operator' after 'fun'
- Omitting 'operator' keyword
- Misordering keywords
data class Point(val x: Int, val y: Int) {
operator fun plus(other: Point) = Point(x + other.x, y + other.y)
}
fun main() {
val p1 = Point(1, 2)
val p2 = Point(3, 4)
val p3 = p1 + p2
println(p3)
}Solution
Step 1: Understand operator plus function
The plus operator adds x and y values of two Point objects and returns a new Point.Step 2: Calculate the result
p1 + p2 = Point(1+3, 2+4) = Point(4, 6). Printing a data class shows property names.Final Answer:
Point(x=4, y=6) -> Option BQuick Check:
p1 + p2 = Point(x=4, y=6) [OK]
- Expecting Point(4, 6) without property names
- Thinking '+' is undefined
- Printing the expression instead of result
class Counter(var count: Int) {
fun operator minus(other: Counter): Counter {
return Counter(count - other.count)
}
}Solution
Step 1: Check operator function syntax
The 'operator' keyword must come before 'fun' in Kotlin.Step 2: Identify the error in code
The code uses 'fun operator' which is incorrect; it should be 'operator fun'.Final Answer:
Incorrect order: 'fun operator' should be 'operator fun' -> Option DQuick Check:
operator keyword before fun = correct syntax [OK]
- Placing 'operator' after 'fun'
- Thinking minus can't be overloaded
- Wrong return type assumption
Vector that supports both addition (+) and multiplication (*) operators. How should you design these operators as functions to keep your code clean and flexible?Solution
Step 1: Understand operator overloading in Kotlin
Kotlin requires operator functions inside the class or as extensions with the 'operator' keyword.Step 2: Choose the clean design
Defining 'operator fun plus' and 'operator fun times' inside Vector class keeps code organized and flexible.Final Answer:
Define 'operator fun plus' and 'operator fun times' inside Vector class -> Option CQuick Check:
Operator functions inside class = clean, flexible [OK]
- Trying to override symbols directly
- Omitting 'operator' keyword
- Using global functions without operator keyword
Ready to prove it?
Pass the test and this pattern moves unknown
