Skip to content

Repository files navigation

suspend-kontext

Maven Central Kotlin License Platform

suspend-kontext is a Kotlin Compiler Plugin that allows you to specify the default coroutine context for suspending functions using annotations.

Motivation

Switching coroutine contexts typically requires the use of withContext, like this:

suspend fun loadText(file: File): String {
    return withContext(Dispatchers.IO) {
        return@withContext file.readText()
    }
}

Combining withContext and return can make your code less readable.

With suspend-kontext, you can simplify this code by using annotations:

@IOContext
suspend fun loadText(file: File): String {
    return file.readText()
}

This approach makes your code more concise and improves readability by explicitly defining the coroutine context at the function level. You can use @IOContext, @DefaultContext, @MainContext, and @UnconfinedContext for now.

Installation

repositories {
    mavenCentral()
}

plugins {
    id("com.kitakkun.suspend-kontext") version "<version>"
}

About

A Kotlin Compiler Plugin that allows you to specify the default coroutine context for suspending functions using annotations.

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages