Functions Taking Other Functions
suggest changeAs 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
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents