https://kotlinlang.org logo
#coroutines
Title
# coroutines
e

egorand

09/04/2018, 3:00 PM
Does anybody have experience with maintaining both coroutine-based and non-coroutine-based codebases in a library? I.e. marking a random library method with
suspend
is a breaking change, since the callers now have to wrap it inside a coroutine builder. Ideally I'd like library users to opt into coroutines if they decide to and be able to use the library as is if they don't, and avoid duplicating code add coroutines support. Any OS projects that do this already?
r

rocketraman

09/04/2018, 3:24 PM
I think the only way which is backward compatible is to have a new coroutines-based suspend function, with the existing signature using
runBlocking
and wrapping the new suspend function.
Callers can continue to use their existing function which transparently uses the builder, or call the new suspending function.
e

egorand

09/04/2018, 3:31 PM
nice, thank you! that's a valid approach, however, the naming also becomes hard, i.e. you'd need to have
retry()
and smth like
retrySuspendable()
, which is not perfect
r

rocketraman

09/04/2018, 3:32 PM
Agreed, naming is an issue.
e

egorand

09/04/2018, 3:34 PM
I think I like
retry()
vs
retryBlocking()
r

rocketraman

09/04/2018, 3:37 PM
That's what I ended up doing in my case, but isn't possible to use that naming to keep things backward compatible for an existing library.
e

egorand

09/04/2018, 3:48 PM
makes sense
p

pakoito

09/04/2018, 3:52 PM
You have
Async
in arrow precisely for this use case of bridging frameworks
ping me privately if you're interested and I can give you an intro and some resources 🙂
r

rocketraman

09/04/2018, 3:56 PM
@pakoito Not the same case as I understand it.
p

pakoito

09/04/2018, 4:11 PM
I'm pretty sure it is, opting into coroutines or running with other abstractions, with assurance there will or will not be thread jumps
and generally avoiding having to explicitly call the coroutines version of
async
or
launch
in libraries
j

Jeremy Rempel

09/04/2018, 6:20 PM
arrow library discusses polymorphic program article which may be helpful: https://arrow-kt.io/docs/patterns/polymorphic_programs/
👍 3
k

kenkyee

09/04/2018, 10:01 PM
Would be a good blog article @pakoito
p

pakoito

09/04/2018, 10:05 PM
the doc @Jeremy Rempel linked sums it all up quite nicely. It’d be hard for me to explain it in fewer words
2 Views