Hi guys, quick question, does `async` executed ins...
# coroutines
t
Hi guys, quick question, does
async
executed inside a
coroutineScope
finishes before the block exits? (i mean,
coroutineScope
waits
async
, just like it does for
launch
)
m
Yes.
g
but only if you start this async using scope of coroutineScope, not some external scope
t
now I was wondering of a compiler plugin or language feature for:
Copy code
val foo: Deffered<Int>
corotineScope {
   foo = async { bar() }
}
foo * 2 // foo is smart casted to Int
g
wait, but why?
I actually even not sure what you trying to achieve, your sample is redundand, you can just do this:
bar() * 2
also even in your example it’s incorrect, it should be like this:
Copy code
val foo: Deffered<Int>
corotineScope {
   foo = async { bar() }
}
foo.await() * 2
but again, it not really needed
t
the idea was
await
not being needed as
async
was called in the block scope
g
why it’s not needed? I don’t understand why you think so
it’s needed, type of foo is incorect
it cannot cast Deferred<Int> to Int
and as I said, I just don’t see why you need corotineScope or async for this example
.await() return immediately, because value of async already there
t
but only if you start this async using scope of coroutineScope, not some external scope
becaus eby your comment, at that stage the
async
has finished, so it can actually be smart casted
and as I said, I just don’t see why you need corotineScope or async for this example
Sorry, could have been a better example, image `bar`takes a while, and there are more stuff in the
coroutineScope
block, including other coroutines being launch/async
g
If you provide such example, we can discuss it, existing example in isolation doesn’t look as something real what should be fixed with a new language feature
👍 1
one more concideration why such “smart cast” is impossible, because Deferred has more information that just value, it also may include Exception and it always has current state
even if corotineScope is returned it doesn’t mean that you have value inside
✔️ 1
👍 1