


This also makes it more explicit from which layers the error came from. If you want to play along, you’ll need to compile with the -Xcontext-receivers flag. In this post, I’d like to toy with them to understand how useful they can be. Here we have defined a service function, that talks to the Persistence layer, and the Network layer.īoth layers have their own error hierarchy, and we would like to keep them separate, so we don’t have to create a single error hierarchy for all layers. / CONTEXT RECEIVER Toying with Kotlins context receivers Kotlin added the idea of Context Receivers in version 1.6.20. The coroutines library provides several elements such as Job, Dispatcher, or Coroutine Name. Internally, it is an interface that defines the operations you could do with it such as query, update, or add. How to convert the function into coroutines.With ( Comparator. A Contextis a map of elements that describes the environment of the coroutine. Then make the property thats dependent on it Lazy. Coroutines share the stack due to support for suspension. 1 Answer Sorted by: 1 You can give it a lateinit context property that you set in your Application class.

It means coroutine suspend execution by returning to the caller and the data that is required to resume execution is stored separately from the stack. It means when one routine is executing, the other routine will not interfere with 1st coroutine w.r.t to memory, cpu ,any resource by suspending itself i.e without blocking.Ĭoroutines don't have a dedicated stack. i.e routines which cooperate with each other. Then whenever you need the context just call application.appContext.
