https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

tmg

05/20/2020, 8:52 AM
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

marstran

05/20/2020, 8:54 AM
Yes.
g

gildor

05/20/2020, 9:02 AM
but only if you start this async using scope of coroutineScope, not some external scope
t

tmg

05/20/2020, 9:28 AM
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

gildor

05/20/2020, 9:28 AM
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

tmg

05/20/2020, 9:33 AM
the idea was
await
not being needed as
async
was called in the block scope
g

gildor

05/20/2020, 9:38 AM
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

tmg

05/20/2020, 9:41 AM
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

gildor

05/20/2020, 9:42 AM
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
4 Views