This lambda with receiver stuff in the kotlinx cor...
# coroutines
g
This lambda with receiver stuff in the kotlinx coroutines API is playing with fire.
d
I would think they return the same exact thing in those suspend functions? The scope receiver and the continuation passed to the top level coroutineContext property at runtime typically are implemented by the same class and thus the same reference.
g
when you create an instance of a
suspend () -> Unit
, needless to say its
coroutineContext
can be greatly varied. Thus:
Copy code
public fun myThing() = runBlocking {
  val x: suspend () -> Unit = { 
    val context = coroutineContext //did you get the invoker of X's context or did you get the runBlocking's context?
  }
}
the answer btw is the runblocking's context, which suprised me, its also difficult because I believe there is no way I can add a label to the anonymous function syntax I'm using
­čś▓ 1
d
That's a sad oversight...
g
I think it might be worthy of a PR, something like
currentCoroutineContext
or
runningCoroutineContext
leave the old one as an alias, but it was inline-intrinsic anyways, so I think that means its name can be changed relatively easily
d
I know it would be the runblockings context... that means you cant access the other one easily. You can create your own function to get what you want:
Copy code
suspend inline fun getCoroutineContext() = suspendCoroutineUninterceptedOrReturn { cont -> cont.context }
But yeah, it should be named better
Probably want to make that inline
g
I was using
suspendCoroutine { it.resume(it.context) }
but yeah, even more simply I can create another inline global static function just to alias
coroutineContext
to something else.
inline suspend fun currentCoroutineContext() = coroutineContext
d
Of course haha
It would be cool if
suspend val
became a thing
Ô׼ 1
­čĄö 1
Im really not sure about
suspend val
. When exactly is something too big to be a property? I donno, I've done a great deal of hand-wringing about
suspend val
.
b
Methods on a receiver scope win over global methods with the same name unless you fully qualify the method name
Is there a reason your lambda is being defined inside of that function instead of simply
suspend fun x() { val context = coroutineContext }
above the
myThing
function?
d
I have a good use case for it but it's elaborate to explain