Skip to content

Instantly share code, notes, and snippets.

@aleksandarzekovic
Last active September 14, 2022 10:21
Show Gist options
  • Save aleksandarzekovic/a612355a05b330d0934fc5ce0d053f86 to your computer and use it in GitHub Desktop.
Save aleksandarzekovic/a612355a05b330d0934fc5ce0d053f86 to your computer and use it in GitHub Desktop.
// The initial state of the state machine
int label = 0
A a = null
B b = null
void resumeWith(Object result) {
if (label == 0) goto L0
if (label == 1) goto L1
if (label == 2) goto L2
else throw IllegalStateException("call to 'resume' before 'invoke' with coroutine")
L0:
// result is expected to be `null` at this invocation
label = 1
// 'this' is passed as a continuation
result = getRandomNum(this)
if (result == COROUTINE_SUSPENDED) return
L1:
A a = (A) result
label = 2
val result = getSqrt(a, this) // 'this' is passed as a continuation
if (result == COROUTINE_SUSPENDED) return
L2:
B b = (B) result
log(String.valueOf(b))
label = -1 // No more steps are allowed
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment