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

groostav

08/09/2016, 7:47 PM
So I've got a general design question. Right now, at any moment, our callstack looks something like this:
Copy code
com.mycompany.OurJavaAndKotlinFrontend.kt:123
sun.jna.proxies
SuperSecretCompanySauce.f99:1234 // yeah thats fortran code
sun.jna.proxies.callback
com.mycompany.OurJavaAndKotlinCallback.kt: 345
now if we assume the three bits of code we control apply transforms A -> B, B -> C, and C -> D respectively --and are all pure and referentially transparent and what not-- can I refactor our existing flow to use co-routines such that that C->D transform is factored out and expressed by the caller,so that the OurJavaAndKotlinCode class contains two methods:
fun generatePartOne(a: A): C
and
setResult(d: D):Unit
To rephrase: right now the
OurJavaAndKotlinFrontend
class contains a method called
fun doWork(input: Input): Output
. It calls our fortran code, converting
input
into an intermediary that our native code can work with. Our native code then calls our JavaCode through a callback. The implementation of that callback was trickey, and I dont want to tamper with it or refactor the sturcture. What I want is that when the fortran code calls back into kotlin with the argument
nativeInfo
, the current callback is suspended and a 'return' is made to the caller of
KotlinFrontend
with the value
nativeInfo
. The caller would do his own processing of
nativeInfo
, converting it into some other object, and then call back our frontend via some method
setResult(convertedNativeInfo: Something)
, which would then resume the native code, passing control flow back to fortran. I'm pretty sure this is achievable with a co-routine, im just not sure what the semantics are and how I would make all of this work under IOC, and with regular threads/executors. Anyone know for certain this is achievable, and how I might go about implementing it? Blog posts much appreciated, even if they use C# verbiage.