Scope overriding continuation result

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.


I don’t have a financial background, so it is pretty hard for me to understand the details, but it doesn’t at all sound like something related to coroutines, but to OOP.

What do you exactly need to change in the way how pv() is executed? Change its parameters, modify its return value, something else?

It sounds confusing to me. How could it actively bypass itself? Do you mean that pv can stop executing itself, including all started subroutines and return immediately?

1 Like

hi broot, thanks for your reply. What I’m looking for is a mechanism that can temporarily (within a scope) mutate/override some suspend function’s value. It turns out I could do this with CoroutineContext and Scope and you are correct it doesn’t really have anything to do with continuation.

I re-read everything you posted here and I think I finally understand, what do you mean. For example, we have functions like :

suspend fun getMultipliedData() : Int {
    return getData() * 2

suspend fun getData() : Int {
    return 5

And you would like to do something like:

suspend fun test() {
    getMultipliedData() // returns 10

    withContext(OverrideGetData(20)) {
        getMultipliedData() // returns 40

Is this what you ask? It sounds like a pure evil if you ask me :wink:

1 Like