This is another feature of Kotlin that is intended for creating DSLs. Infix functions are like normal functions, but they can be called without the dot and parentheses. To declare an infix function, you have to place the infix
keyword before the fun
keyword. Here's an example:
class Employee { infix fun payout(salary: Int) { print("Employee was paid: $salary") } }
Now, we can call the payout
function without the dot and parentheses:
val employee = Employee() employee payout 3500
These conditions have to be satisfied to use the infix
keyword:
- Function has to have a single parameter
- Function has to be a member function or an extension function
- Parameters cannot be variable arguments
- Parameters cannot have a default value