timrijckaert
07/01/2024, 6:11 PMsuspend fun
can we declare that it will never throw?
I want to get rid of the try await
and instead just have await
.
Is this possible?
data class Thing(val item: Int)
class ThingRepository {
suspend fun getThing(succeed: Boolean): Thing {
delay(100.milliseconds)
return Thing(0)
}
}
Task {
let thing = await ThingRepository().getThingSimple(succeed: true)
print("Thing is \(thing).")
}
timrijckaert
07/01/2024, 6:25 PMMichael Krussel
07/01/2024, 8:33 PMCancellationException
is always added to the list of exceptions that can be thrown. I don't think there is any way to prevent that. All the other exceptions are fatal unless you mark the function as throwing.
The decision to convert that doesn't fully make sense. I assume the scope that is being used to launch the actual suspending function cannot be canceled, and the API doesn't expose a way to cancel the job. That only leaves Rogue Cancellation exceptions (https://betterprogramming.pub/the-silent-killer-thats-crashing-your-coroutines-9171d1e8f79b), which I think should be treated like any other exception. But I'm not really sure how the suspend function calling from Objective-C works.jw
07/02/2024, 4:12 AMMichael Krussel
07/02/2024, 12:14 PM* @note This method converts instances of CancellationException to errors.
* Other uncaught Kotlin exceptions are fatal.