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

akshay_b

12/11/2019, 7:47 AM
Hi, I have the following suspended function, which I want to expose as Java APIs(blocking) and Kotlin API(non-blocking) with same method name.
Copy code
suspend fun getStoreMapping(country: String): List<Store> {
    return client.getStoreMapping(country).stores
}
Currently, the only option I have is to use different method name, which calls the suspended function in
runBlocking {}
like below -
Copy code
fun getStoreMappingInJava(country: String): List<Store> {
    return runBlocking { client.getStoreMapping(country).stores }
}
o

octylFractal

12/11/2019, 7:50 AM
you might be able to add
@JvmName
to the second one and have that work
that way, the kotlin names are different, but since the jvm signature is different, the compiler will let you name it the same thing
https://pl.kotl.in/q2J8Y2uBe appears to compile
Copy code
public final class TestKt {
  public static final java.lang.Object suspender(kotlin.coroutines.Continuation<? super kotlin.Unit>);
  public static final void suspender();
}
looks good to me
g

gildor

12/11/2019, 12:18 PM
I prefer to call such functions with Blocking suffix to make it explicit that function will block caller thread
a

Adam Powell

12/11/2019, 1:58 PM
It would probably be more useful to expose a future, or callbacks, or Rx Single/Maybe for java callers so that they don't have to block a thread
a

akshay_b

12/13/2019, 9:01 AM
Thanks all for the input 🙂 .
2 Views