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

Manuel Vivo

03/17/2020, 9:15 AM
For people interested in what’s doing the Kotlin compiler under the hood 👌 Blog post about this coming along next week

https://www.youtube.com/watch?v=IQf-vtIC-Uc&list=PLWz5rJ2EKKc_T0fSZc9obnmnWcjvmJdw_&index=5

💪 1
👍 8
s

stevecstian

03/17/2020, 9:18 AM
The return type of the transformed function is actually
Any?
, not
Unit
, right?
m

Manuel Vivo

03/17/2020, 9:45 AM
It’s
Unit
, there’s nothing to return
the
completion
param is used to “return” a value
s

stevecstian

03/17/2020, 9:51 AM
Have you checked https://jakewharton.com/exceptions-and-proxies-and-coroutines-oh-my/#coroutine-implementation-crash-course ?
completion
seems not the only way to "return" value...
m

Manuel Vivo

03/17/2020, 9:54 AM
Wonder if it’s because of the use of
suspendCoroutine
s

stevecstian

03/17/2020, 9:59 AM
I believe @elizarov explained it in this talk before:

https://youtu.be/3xalVUY69Ok

, but I can't find the slide now...
j

jw

03/17/2020, 1:49 PM
All suspend functions use
Any?
as a return type because it's a union type of
T | COROUTINE_SUSPENDED
👍 1
This allows code like
Copy code
suspend fun foo(): String {
  return if (Random.nextBoolean()) {
    4
  } else {
    otherSuspendFunction()
  }
}
to return synchronously when it can. Synchronous exceptions behave similarly.
❤️ 1
m

Manuel Vivo

03/17/2020, 2:28 PM
Thanks @jw! Will add that to the the post 👌
a

araqnid

03/17/2020, 3:54 PM
I have an example of calling suspend functions from Java at https://github.com/araqnid/library-versions/blob/master/java/src/main/java/org/araqnid/libraryversions/java/JavaMain.java which illustrates the call interface, if it’s helpful
5 Views