Pepe's Braindump

Kotlin suspend

tags
Programming Languages

Suspend

Suspend functions are a way of writing async code in a handy way. Under the hood, the compiler rewrites it to a continuation passing style.

under the hood

under the hood, the compiler converts suspended functions:

suspend fun potato(int: Int): Int {

}

into normal functions that have an additional parameter, a Continuation<T> .

fun potato(int: Int, continuation: Continuation<Int>): Int {

}

Continuations model computations that can be paused and resumed. The same continuation will be passed over through all the suspended functions unless a fork happens, in which a new continuation will need to be created and passed.

Glossary

Continuation

The name given to the callback that suspended functions call when they’re done.

CoroutineContext

The object, part of Continuation that contains the state of the computation.

ContinuationInterceptor

Used for dispatching coroutines to different threadpools.

Cortex theme by Jethro Kuan. Built with org-mode, org-roam and Hugo