Kotlin infix

Example

Add new add operation keyword

1
2
3
4
5
6
7
8
infix fun Int.add(x: Int): Int {
return this + x
}

fun main() {
val result = 100 add 200
println(result)
}

Add add operation to Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Account(
private var balance: Double = 0.0
) {
infix fun add(amount: Double): Double {
return this.balance.plus(amount)
}
}

fun main() {
val account = Account(10.0)

val result = account add 1.0
println(result)
}
// result is 11.0