Is there a way to write a higher-order function wh...
# coroutines
m
Is there a way to write a higher-order function which is a suspend function if you pass a suspend function argument and non-suspend function if you pass a non-suspend function argument? Basically like Swift’s `rethrows`:
Copy code
func someFunction(callback: () throws -> Void) rethrows {
    try callback()
}
If
callback
is marked
throws
then
someFunction
is also marked
throws
, otherwise it’s not. That would allow for nice reusable code like this:
Copy code
inline suspend? fun <T, R> Iterable<T>.map(transform: suspend? (T) -> R): List<R> { // syntax TBD - maybe doesn't even need special syntax
	val transformedElements = mutableListOf<R>()
	for (element in this)
		transformedElements += transform(element) // either suspend or not
	return transformedElements
}
If
transform
is suspendable then the whole function is suspendable, otherwise it’s not.