Hi, is it technically possible to resume continuat...
# coroutines
a
Hi, is it technically possible to resume continuation twice when calling suspendCoroutine { cont -> cont.resumeWith(...) }?
d
No! It must only be done once. The second time would throw an exception I think. Do you need this?
a
I am just thinking what capabilities would emerge if multiple returns would be possible. It will work as some kind of re-entrant call of function (which is parent program), it's hard to imagine mentally, but if it's possible maybe we could create something like Jetpack Compose without compiler addons.
d
Definitely not supported and dangerous
a
why it would be dangerous?
By reading some examples where CPS could be applied there are interesting cases: as backtracking or others:
s
Use Flow for multiple value emissions. If you're thinking about for-comprehension like or do-notation like functionality, the folks from Arrow actually did make a form of multi-shot continuation possible. Not sure if their current Fx library still does that or now uses a different approach.
d
Not considering Arrow, coroutine code abstracted by continuation contains a state machine based on your code, which is predicated on running exactly once. Therefore, if it runs more than once on the same state machine you get undefined behaviour.
☝️ 2