Peter Kievits
10/22/2024, 9:45 AMsuspend
function, which our InvocationHandler
then wraps and ends up calling another function marked with suspend
. I found this article on how handle dynamic proxies, however it seems to be calling the same method on a different implementation of the same interface. Whereas I'm trying to map to a different class. Is there a way I can pass in the continuation provided by the caller onto the suspend function from kotlin? I image I could do it from java, but I would rather not write any java code. Thanks!Youssef Shoaib [MOD]
10/22/2024, 10:46 AMkotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
is likely what you wantPeter Kievits
10/22/2024, 10:51 AMsuspend
. I will need to make the call from a non-suspend
function, the InvocationHandler
.Youssef Shoaib [MOD]
10/22/2024, 10:53 AMcreateCoroutineUnintercepted
or something like thatPeter Kievits
10/22/2024, 11:36 AMPeter Kievits
10/22/2024, 11:37 AMInvocationHandler
?Youssef Shoaib [MOD]
10/22/2024, 11:38 AMstartCoroutineUninterceptedOrReturn
might be better then. Basically, coroutines have an optimization where, if they don't need to suspend, they'll return their value immediately, but if they need to suspend, they return a special marker value COROUTINE_SUSPENDED
. So doing the orReturn
version plugs in well with that optimizationPeter Kievits
10/22/2024, 11:40 AMPeter Kievits
10/22/2024, 11:40 AMYoussef Shoaib [MOD]
10/22/2024, 11:41 AMstartBlahOrReturn
should do the same exact trick as wellPeter Kievits
10/22/2024, 11:50 AMPeter Kievits
10/22/2024, 11:55 AMPeter Kievits
10/22/2024, 11:55 AMjw
10/22/2024, 12:28 PMYoussef Shoaib [MOD]
10/22/2024, 12:43 PMyield
is a no-op if you use Unconfined as your dispatcher or if you have a custom continuation interceptor that doesn't offer dispatching. I'll see if I can reproduce that.jw
10/22/2024, 12:50 PMjw
10/22/2024, 12:50 PMYoussef Shoaib [MOD]
10/22/2024, 12:51 PMPeter Kievits
10/22/2024, 1:00 PMUndeclaredThrowableException
while debugging at some point. Will have to dig a little deeper to make sure those are handled correctly.Peter Kievits
10/22/2024, 2:55 PMUndeclaredThrowableException
in there either. Suspending and resuming with the exception that was already thrown, probably comes with a cost, but it looks like it's either that or an UndeclaredThrowableException
.jw
10/22/2024, 2:57 PMjw
10/22/2024, 2:58 PMcatch
blockjw
10/22/2024, 2:58 PMPeter Kievits
10/22/2024, 3:02 PMsuspendCoroutine
?jw
10/22/2024, 3:13 PMtry
can throw synchronously, then yesPeter Kievits
10/22/2024, 3:20 PMUndeclaredThrowableException
at that point?Peter Kievits
10/22/2024, 3:29 PMyield()
call and then using the startCoroutineUninterceptedOrReturn
call. However, the stacktraces are the same in this code, and the code above.