Good Evening, (or whatever your local time is) i s...
# coroutines
m
Good Evening, (or whatever your local time is) i stumbled across a phenomenon i couldn't explain to myself. I am currently working on a Kotlin HTTP server and when running following Code:
Copy code
var testScope: CoroutineContext?
    get() = null
    set(value) {}

fun Route.kotlinSuperiority() {
    get {
        testScope = this.coroutineContext
        println(testScope)
        println(this@get.coroutineContext)
        println("why?")
    }
}
the results are
Copy code
null
[io.ktor.server.engine.DefaultUncaughtExceptionHandler@1f751406, CoroutineName(request-handler), ScopeCoroutine{Active}@22ddec7a, <http://Dispatchers.IO]|Dispatchers.IO]>
why?
so my question is why my outer scope variable isn't beeing set to the CoroutineContext (and if there is a way to workaround). I could only imagine some weird threading phenomenon i don't understand. Thanks ~Moritz (and i hope i am in the right channel)
a
Your setter is a no-op, you would need to add
field = value
somewhere in the setter body in order for the backing field to be updated, which also means it needs to be initialized with
= null
. Same thing with the getter, it’s always returning null. If the get/set accessors aren’t actually doing anything other than getting/setting the field then you really don’t need them at all and can just have
var testScope: CoroutineContext? = null
.
1
👍 1
m
Oh wow, yep makes sense, now everything works. Thank You :)