CoroutineScope interface declares only coroutineContext property. All functions (e. g., cancel()) are declared as extension functions instead of interface functions.
Any ideas why this decision was made? What are the benefits of this?
CoroutineScope interface declares only coroutineContext property. All functions (e. g., cancel()) are declared as extension functions instead of interface functions.
Any ideas why this decision was made? What are the benefits of this?
If cancel
is a method in the CoroutineScope
interface, it means that the procedure for cancelling all the coroutines in a scope is private, subject to change, and might be different in every CoroutineScope
. The only reliable way to do it is to call the interface method. Implementers of CoroutineScope
need to consider whether or not the default implementation is appropriate for them and override it if necessary.
If cancel
is an extension function on CoroutineScope
, it means that you can cancel all the coroutines in a CoroutineScope
using only the methods that already exist, based on what they are already required to do. You could implement a modified cancellation procedure if you wanted to. The provided cancel
implementation will work on any valid CoroutineScope
, current or future, and implementers of CoroutineScope
don’t need to consider it at all.