https://kotlinlang.org logo
Title
t

thana

01/04/2022, 10:50 AM
hi, i just read https://maxkim.eu/things-every-kotlin-developer-should-know-about-coroutines-part-2-coroutinescope which says that one shouldn't implement the
CoroutineScope
interface anymore. unfortunately it doesn't say anything about the WHY despite using the builder function would be more straightforward. why is it more straightforward?
n

Nick Allen

01/04/2022, 10:34 PM
Unless external code launching coroutines is part of your use case, you probably don't want to implement
CoroutineScope
. Do you really want people to do this:
val foo = MyClassThatUsesCoroutinesInside()
foo.launch { ... }
t

thana

01/05/2022, 11:17 AM
but why?
n

Nick Allen

01/06/2022, 6:37 AM
https://en.wikipedia.org/wiki/Encapsulation_(computer_programming) Code is easier to maintain when private details are not exposed to callers.
t

thana

01/06/2022, 8:48 AM
oh so its all about the observability wether or not the class implements the interface
n

Nick Allen

01/06/2022, 6:52 PM
Yeah, implementing an interface just so you can use it internally often considered an anti-pattern.