Skip to content

Kotlin's invoke Operator function

Rajesh Khadka edited this page Dec 7, 2018 · 2 revisions
class ReleaseHandler {
    operator fun invoke() {
        print("invoke method called")
    }
}

var release = ReleaseHandler()

Output: invoke method called

If you want to execute a function without specifying the function name then you can define invoke the function as:

operator fun invoke() {}

This will be a great trick in case of a class having the single method.

References:

  1. https://dzone.com/articles/kotlin-dsl-basics
  2. http://joshskeen.com/kotlins-invoke-operator/
Clone this wiki locally