Unresolved reference: async in Kotlin in 1.3
The problem is that async
(the same as launch
) is defined as an extension function on CoroutineScope
. In the following example, it is invoked in the receiver CoroutineScope
of withContext
:
suspend fun computecr(array: IntArray, low: Int, high: Int): Long {
return if (high - low <= SEQUENTIAL_THRESHOLD) {
(low until high)
.map { array[it].toLong() }
.sum()
} else {
withContext(Default) {
val mid = low + (high - low) / 2
val left = async { computecr(array, low, mid) }
val right = computecr(array, mid, high)
left.await() + right
}
}
}
First of all you have to remove from Gradle the part where you enable the experimental coroutine feature.
kotlin {
experimental {
coroutines = Coroutines.ENABLE
}
}
You cannot use async()
function implicitly anymore. You have to call it explicitly for a global scope coroutine GlobalScope.async(){...}
or you can call it from another coroutine scope using CoroutineScope(...).async{...}
or from scoping functions coroutineScope {...}
, withContext(...){...}
.
I have written an example for personal use to understand myself how coroutines are working. I hope it is good and helpful.