How to Enable Context Receiver in Kotlin multiplatform?

I tried to do this:

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs += listOf("-Xcontext-receivers")
    }
}

but it din’t work.
How to enable it properly in kotlin multiplatform project?

1 Like

I don’t have an example handy right now, but I’d be surprised if it’s not documented here: Compiler options in the Kotlin Gradle plugin | Kotlin Documentation

As far as I know, the entire context-receivers feature seems to only be supported by the JVM platform as of now. Meaning, given your build-script, it should only work in the jvmMain and jvmTest source set respectively.

I might be wrong though.

1 Like

I’ve enabled by this:

kotlin {
    sourceSets {
        val main by getting
        main.dependencies {
            implementation(libs.compose.resources)
        }
        main {
            compilerOptions {
                freeCompilerArgs.add("-Xcontext-receivers")
            }
        }
    }
}

The project was only for desktop

Just as a heads up, context receivers won’t end up in Kotlin. The new concept is called “context parameters”, it’s similar (basically context receivers with named arguments) but unfortunately it’s not yet available, not even experimental.

1 Like

It’s actually available in 2.1.20-Beta1 I believe (Definitely a beta version for 2.1.20) with the argument -Xcontext-parameters. It sadly has no IDE support yet though

1 Like