elizarov
01/10/2017, 10:30 AMval v by asyncLazy { … }
working in 1.1 time-frame, but the simpler syntax of val v = asyncLazy { … }
is already working in 1.1-M04. The only downside is that you have to use v.value()
instead of just v
.
When the former syntax by asyncLazy
becomes possible, this property will have a suspending getter and every attempt to read this property will have to be inside a coroutine. However, if you are not in a coroutine, it is always easy to create one (that is the whole point of coroutines — they are extremely lightweight) and this gives you call-site visibility on what your actual asynchronity policy is. For example, if you are not in a coroutine and you want to read asyncLazy
value and you want to block your thread while it is being computed, you just do blocking { v }
(or blocking { v.value() }
for now). This way that fact that you are actually blocking a thread becomes explicit. Compare this to regular lazy
properties where thread-blocking is implicit and invisible side-effect that can potentially bite you on such an innocent-looking action as reading a property.