Mixing synchronous and asynchronous code is bad, ok.
Calling blocking code from coroutine is really bad, ok.
Unfortunately Kotlin allow using regular function inside suspending function, we should avoid this?
Probably not, for compatibilty reason with Java.
We should handle this?
Something like:
suspend fun foo(inputStream: InputStream) {
...
val data = run(IO) { inputStream.read() }
...
}
But this rule should be enforced for all Java invocations.
On other side regular function cannot call coroutines, now.
But calling coroutines from regular functions isn’t so bad as above.
So blocking code could call coroutines in blocking mode, ie:
import kotlinx.coroutines.experimental.sync.Mutex
fun baz(mutex: Mutex) {
mutex.lock()
...
mutex.unlock()
}
This default allows better integration between regular function and coroutines.