这个it应该不是被包装SafeContinuation吧,我看源码里,startCoroutine其实就是下面这段代码
createCoroutineUnchecked(completion).resume(Unit)
然后createCoroutine的文档说了这个SafeContinuation的resume只能被invoke一次:
/**
* Creates a coroutine without receiver and with result type [T].
* This function creates a new, fresh instance of suspendable computation every time it is invoked.
* To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance.
* The [completion] continuation is invoked when coroutine completes with result or exception.
* Repeated invocation of any resume function on the resulting continuation produces [IllegalStateException].
*/
上面的代码文档说了resume只在我们启动创建的携程(也就这个SafeContinuation)使invoke。之后的resume invoke会抛出异常, IllegalStateException. 这个例子里面startCoroutine已经invoke了resume,如果再在a里面resume应该会抛出异常。
然后我又跑去看了Roman在kotlinConf上的deep dive into coroutine talk,这个it其实应该是指传进a里的那个state machine (其实也是一个continuation的实现),编译器在suspen lamba开头隐式生成的。