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

noncom

07/10/2018, 1:46 PM
I want to use a language interpreter written for Java (LuaJ) and allow it to launch program execution (interpretation) on Kotlin coroutines. As far as I understand, it is impossible to get that functionality by just using the library from Kotlin. What would be the easiest way to make it work with Kotlin?
g

gildor

07/10/2018, 1:55 PM
Kotlin Coroutines cannot be used directly (with suspend) outside of Kotlin source code. Because it's compile time transformation of suspend code to state machine. But you can write some async code using coroutines and than use some callback/promise/future based wrapper to call this code outside of Kotlin
n

noncom

07/10/2018, 1:57 PM
hmmm, so what about my proposed solution of turning the whole codebase of LuaJ into Kotlin from Java and making all the functions
suspend
?
I think that should work... but maybe there are better ways or reasons why it would not be a good idea..
g

gildor

07/10/2018, 1:59 PM
Yes, it would work
But why all the functions of your code base should be suspended?
Are they asyncronous? But in this case you current code should use something like call backs, so convertation will not be so easy
n

noncom

07/10/2018, 2:05 PM
hmmmmm
that's interesting actually.. I have to think about it. Maybe I simply need to make only particular functions suspeneded, the ones that are triggered by the interpreter and do most of the work
the HTTP and DB calls and stuff I mean
maybe the interpreter itself can stay as it is
g

gildor

07/10/2018, 3:18 PM
Wrapping http or db is make sense, but if your goal is scalability and reduce amount of threads, in this case you also should use asyncronous non-blocking http and db API wrapped to coroutines. Or course, you also can wrap blocking API to coroutines, but it will be like wrapping blocking calls to threads, nothing better in terms of scalability than blocking calls
In other thread you mentioned that you use ktor, so you also can use non-blocking ktro http client
6 Views