Functions Taking Other Functions

suggest change

As seen in “Lambda Functions”, functions can take other functions as a parameter. The “function type” which you’ll need to declare functions which take other functions is as follows:

# Takes no parameters and returns anything
() -> Any?

# Takes a string and an integer and returns ReturnType
(arg1: String, arg2: Int) -> ReturnType

For example, you could use the vaguest type, () -> Any?, to declare a function which executes a lambda function twice:

fun twice(x: () -> Any?) {
    x(); x();
}

fun main() {
    twice {
        println("Foo")
    } # => Foo
      # => Foo
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents