edrd
02/09/2022, 3:50 PMclass Logging {
val logger = java.util.logging.Logger.getLogger("test")
}
context(Logging)
fun hello() {
<http://logger.info|logger.info>("Hello")
}
fun <C, R> within(ctx: C, call: context(C) () -> R): R = call(ctx)
fun main() {
within(Logging()) {
hello() // Error: No required context receiver found: Cxt { context(Logging) public fun hello(): kotlin.Unit defined [...]
}
}
Is this supposed to be this way or it's just something not yet implemented in the prototype version?within
to use a standard receiver instead of a context receiver works, though.
fun <C, R> within(ctx: C, call: C.() -> R): R = call(ctx)
However I wanted to try lambdas with multiple receivers so to implement with
functions for more than one receiver.Oliver.O
02/09/2022, 6:52 PMclass Logger {
fun info(message: String) {
println("INFO: $message")
}
}
class Logging {
val logger = Logger()
}
context(Logging)
fun hello() {
<http://logger.info|logger.info>("Hello")
}
fun <R> within(ctx: Logging, call: context(Logging) () -> R): R = call(ctx)
fun main() {
with(Logging()) {
hello()
}
within(Logging()) {
hello() // Error: No required context receiver found: Cxt { context(Logging) public fun hello(): kotlin.Unit defined [...]
}
}
The above seems to happen if context receivers are not enabled for the compiler. Could you try this build.gradle.kts
?
@file:Suppress("UNUSED_VARIABLE")
plugins {
kotlin("multiplatform") version "1.6.20-M1"
application
}
repositories {
mavenCentral()
}
kotlin {
jvm {
compilations.all {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += listOf("-Xcontext-receivers")
}
}
withJava()
}
sourceSets {
val jvmMain by getting
val jvmTest by getting
}
}
application {
mainClass.set("MainKt")
}
Hanno
02/09/2022, 8:24 PMOliver.O
02/09/2022, 10:00 PMwithin
. As soon as I re-introduce it (C
), the error reappears. So yes, seems to be a bug.Anastasia Shadrina
02/10/2022, 10:41 AM