Another important aspect of higher-order functions is the ability to return functions from other functions. Like defining a function type for a variable or function argument, we can define function return types as well.
One example of where this could be useful would be in writing a factory function that returns a different function for creating values based on some input.
In this example, we've created a getGreetingProvider function, which takes a Boolean argument and returns a function that returns List<String>:
fun getGreetingProvider(isFriendly: Boolean) : () -> List<String> {
return if (isFriendly) {
{ listOf("Hey", "Hi", "Hello") }
} else {
{ listOf("Go away", "Leave me alone") }
}
}
Within the function block, we can then return a different function depending on the isFriendly argument. Both functions must adhere to the defined return type, but they are free to return any function that matches the signature.