Infix Function Call Kotlin
In Kotlin, you can declare functions as infix
functions, which allow you to call them using infix notation. Infix notation means that you can call the function using the function name, followed by the argument, followed by the object on which the function is called, all separated by spaces. Here's an example:
infix fun Int.multiplyBy(x: Int): Int { return this * x } fun main() { val result = 5 multiplyBy 10 println(result) }
In this example, we have defined an infix
function called multiplyBy
as an extension function of the Int
class. The multiplyBy
function takes one input parameter x
of type Int
and returns an Int
value.
In the main
function, we call the multiplyBy
function using infix notation. We first provide the argument 10
, and then we provide the object on which the function is called, which is 5
. The multiplyBy
function multiplies the argument 10
with the object 5
and returns the result 50
. The result is then assigned to the result
variable, and printed to the console.
The output of this program is:
50
Note that infix notation can only be used with functions that have a single input parameter.