Just me or does this not make sense? How can it re...
# android
z
Just me or does this not make sense? How can it return from within withContext?
The only way I can make this code work is as follows:
Copy code
override suspend fun doWork(): Result = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
  // code here
  return@withContext Result.success()
}
z
Yep, non-local returns are only allowed from inline functions
e
you can also leave off the
return
altogether, as a (non-Unit) lambda returns its final expression unless early-return occurs:
Copy code
override suspend fun doWork: Result =
    withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
        val data = downloadSynchronously("<https://www.google.com>")
        saveData(data)
        Result.success()
    }
a
or if you want to keep brackets you can do this
Copy code
override suspend fun doWork() : Result {
   return withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
        Result.success()
   }
}
z
I guess I should have added that the qualified return is not only a possible way to do it, it is, in my opinion, preferable. If you have more than a single expression in a lambda, it’s much harder to read when there’s no explicit return imo.
true story 1