turastory
08/14/2020, 1:49 AMsuspend operator
soon or later? I’d like to do something like this:
suspend inline operator fun <T> SuspendableLazy<T>.getValue(thisRef: Any?, property: KProperty<*>): T
= ...
private val value by suspendableLazy { getValue() }
fun test() = println(value)
private suspend fun getValue() { ... }
octylFractal
08/14/2020, 1:50 AMvalue
in here
if you want this, get an appropriate CoroutineScope and do private val value = scope.async(start = CoroutineStart.LAZY) { getValue() }
turastory
08/14/2020, 2:11 AMvalue
🙏
But we can handle that by implementing SuspendLazy<T> like this:
class SuspendableLazy<T>(scope: CoroutineScope, block: suspend () -> Unit) {
suspend fun get(): T = scope.async { block() }.await()
}
suspend inline operator fun getValue(...) = await()
private val value by suspendableLazy(scope) { getValue() }
octylFractal
08/14/2020, 2:12 AMvalue
needs to be some sort of private suspend val
as well, but that's not something that really exists yet. I don't know if it ever will, either
it still wouldn't help with your use case of using it in a non-suspend functionoctylFractal
08/14/2020, 2:13 AMsuspend val
, it's coroutineContext
, but that's a special intrinsic value, not a real feature)turastory
08/14/2020, 2:22 AMsuspendable property
or not.
(By the way, my first code snippet is incorrect! - I didn’t mean to use value
in non-suspend function)