Invoke Operator in Kotlin Hindi

Invoke Operator in Kotlin Hindi

Here are some expressions using invoke operator with corresponding functions in Kotlin.

ExpressionTranslated to
a()a.invoke()
a(i)a.invoke(i)
a(i1, i2, ..., in)a.inkove(i1, i2, ..., in)
a[i] = ba.set(i, b)

In Kotlin, parenthesis are translated to call invoke member function.

 

Example

class Example{
 operator fun invoke(x:Int){
 println("You are in invoke function with value of x ="+x)
 }
 }

fun main(args: Array<String>) {
 var e:Example = Example()
 e(10)
 }

Output:

You are in invoke function with value of x = 10