Hello, question from newbee. I would like to creat...
# compiler
g
Hello, question from newbee. I would like to create a Kotlin subset that is resumable, i.e. whose internal state can be saved in a database at specific points in the code to be resumed later - perhaps on another machine. Will I have to rewrite everything, or are there parts I can reuse (beyond the grammar)?
r
image.png
That's a fundamental part of a language, I only remember Apache Spark doing something similar in Scala, and it required an unholy amount of black magic to make work - which is why they always lagged a language version behind the compiler to make it work.
You could probably rewrite it to some representation via KSP, and then evaluate that in a virtual machine you control.
Or take a look at Elixir which has these features built-in 🙂
👀 1
i
You can expand current coroutines machinery - every suspend function has a continuation object, which (in theory) can be saved and restored on different machine. Be careful, though, since not everything is serializable. So, in theory, there is a subset of Kotlin with only
@Serializable
objects and consisting only of suspend functions. The only thing you need is to write serialization/deserialization routines.
👀 1
g
That’s the idea actually. Can you please point me out where I should start looking at?
In this subset, we would have only data coming from protobuf objects - hence serializable
i
@Gilles Barbier Depends on your knowledge. Here is the section about variables spilling (saving to an object for you to parse) https://github.com/JetBrains/kotlin/blob/master/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/coroutines-codegen.md#variabl[…]pilling, you should save the state of the program in https://github.com/JetBrains/kotlin/blob/master/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/coroutines-codegen.md#suspendcoroutine and then, after restoring the state, just call
Continuation.resume()
on the restored object.
🙌 1