Dariusz Kuc

04/11/2019, 5:54 AM
hello, I got a prototype bean that is created at runtime based on some input parameters. Within the prototype bean I got multiple methods that accept value (and some others that dont) that should be computed just once (as it is pretty expensive). Since I don't want to incur the initialization penalty if I'm not going to be calling a method that needs it I was using
by lazy
. I'm migrating now to coroutines was wondering whats the proper way to update
by lazy
to call suspendable function. Any thoughts on how to make this better?


06/07/2019, 6:31 AM
I’m also interested in this. Did you find out anything? Maybe of interest:

Dariusz Kuc

06/07/2019, 1:48 PM
lazy delegation doesn't fit well into coroutine world
simplest equivalent is to just create lazy deferred, i.e.
async(start = CoroutineStart.LAZY)
and just await on it from the places
with the example above it would simply become
private val myDeferred = GlobalScope.async(start = CoroutineStart.LAZY) {
    suspend fun functionA() = myAnotherService.firstMethod(myDeferred.await())
    suspend fun functionB() = myAnotherService.secondMethod(myDeferred.await())
👍 1
*noted you probably want to create your own
instead of a global one