expensivebelly
02/15/2022, 8:25 AMCallable
in a coroutine?Joffrey
02/15/2022, 8:27 AMCallable
? What is your goal more precisely?expensivebelly
02/15/2022, 8:32 AMget(K key, Callable<V> valueLoader
) but I would like to be able to call it with this signature: suspend fun get(key: K, valueLoader: suspend () -> V
)Joffrey
02/15/2022, 8:42 AMCallable
in a synchronous way. It blocks its current thread in order to get the value, so there is no way you can provide this value asynchronously and lazily at the same time. The solution for this part would be to use `runBlocking`:
fun get(key: K, valueLoader: suspend () -> V) = get(key, Callable { runBlocking { valueLoader() } })
You wanted your new function to be suspend
probably because you want to express the asynchronism for this. The problem is that the original get
is synchronous, so you cannot really wait for its value in a suspending way. With runBlocking
, you don't really have to anyway.Sam
02/15/2022, 8:45 AMSam
02/15/2022, 8:45 AMexpensivebelly
02/15/2022, 8:47 AMSam
02/15/2022, 8:47 AMexpensivebelly
02/15/2022, 9:26 AMexpensivebelly
02/15/2022, 9:26 AM