Is there a way to convert a suspend function refer...
# coroutines
p
Is there a way to convert a suspend function reference to non suspend one? Lets say I have two functions:
Copy code
suspend fun some(argument: Int) : Unit {
	// process
}

fun service(some: (Int) -> Unit) {
    some(1)
}
And I'd like to pass
some
by reference like this:
Copy code
val coroutineScope = ...

service(::some::launchIn(coroutineScope))
The function
launchIn(coroutineScope)
is what I'm looking for, am I missing something built in, or is there a way I can create it?
j
There is not built-in way to do that, at least not to my knowledge. But why do you need a reference here? Why not just pass a lambda?
Copy code
service { arg ->
    coroutineScope.launch {
        some(arg)
    }
}
Also note that launching a coroutine like this might not always be desirable, because maybe the higher order function (e.g.
service
here) relies on the function being called in place, but here you are launching it asynchronously
p
I know that I can pass a coroutine function when I need to wait to be finished, but when there're cases when I don't need it.
service
shouldn't decide on which scope to run this function. When I have many of arguments like this, the code looks like a mess. All I really need to see when reading this code is which function will be called and on which scope. Having 5 lines instead of 1 doesn't looks nice. This's what my
launchIn
is trying to solve.
j
You could extract that lambda to its own function and reference that one, or to a variable and use the variable instead of a reference. Or you could write your own utility for it if really you need that. But I find the requirements a bit strange, here. It seems a bit fishy that a higher order function like this takes many function arguments that can potentially be called in different scopes, and without control as to whether they are synchronous or asynchronous