Infix Functions
Readable function calls.
What Is an Infix Function?
An infix function can be called without the dot or parentheses: a func b. Mark it with the infix keyword.
infix fun Int.times2(factor: Int) = this * factor
fun main() {
println(3 times2 4)
}Rules for Infix
An infix function must be a member or extension function, take exactly one parameter, and that parameter must not be vararg or have a default.
infix fun Int.plusBy(other: Int) = this + other
fun main() {
println(5 plusBy 7)
}All lessons in this course
- Infix Functions
- Building DSL-like APIs
- tailrec Functions
- When to Use Each