Hi community, I’m developing a financial risk calculation system using Kotlin. In particular, I’m heavily relying on coroutine to achieve asynchronous computation (overlap IO and CPU). Besides, the suspend function makes it possible to separate business logic from technical concern (it’s crucial in our use case because people who write code might come from business and don’t have enough technical expertee). In addition to asynchronous, I have another use case that might rely on intercepting/scheduling function calls in a non intrusive way.
Here’s the detail. The main functionality of my system is to compute risks of financial products. Let’s say I have a function present_value that computes the value of a priceable. And a risk can be defined by measuring pvs of different scenarios.
suspend fun pv(priceable):Double { … } // might have a huge callgraph underneath, e.g. say it calls a funciton yieldCurve in this case
suspend fun risk(priceable):Double {
val pv1 = given(scenario1) { pv(priceable) }
val pv2 = given(scenario2) { pv(priceable) }
val risk = compute(pv1,pv2)
return risk
}
Here 'given(scenario)’ intends to declare a scoped mutation/override of some underlying function result that pv depends on. For example I might say given(yieldCurve := xxx) { pv(priceable) }, when pv calls yieldCurve it will bypass its execution and uses the tweaked value instead.
Kotlin is pretty new to me and as far as I understand coroutine and continuation, it seems Kotlin has mechanisms here and there to implement this, like CoroutineContext/Scope, ContinuationInterceptor etc, but I cannot really connecting the dots myself. Therefore I’d like to ask the community to see whether it’s possible to do this in KT continuation or maybe there’s some other alternatives.
Thanks