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

Joris PZ

10/25/2018, 1:48 PM
I'm building a library that uses co-routines and channels internally. As I see it, I have a number of options when designing my public API: • Offer an API that exposes suspending functions (which in turn uses the internal Channel structure) • Hide my co-routines completely, and offer a blocking API to the clients • Provide two separate API classes (
class XYZ
and
class BlockingXYZ
?) • Provide a single API class with both suspending and blocking versions of my methods, i.e.
suspend fun doABC()
and
fun doABCBlocking()
Are any of these choices considered idiomatic? Or should I just do what feels right to me?
a

arocnies

10/25/2018, 1:58 PM
Do you want your API to be usable from Java? I've heard it said that suspending functions should be named normally
suspend fun abc()
and if you want to provide Futures/Promises/Deferreds you would name them
fun abcAsync()
j

Joris PZ

10/25/2018, 2:01 PM
Good point. No, Java interop isn't important in this case, though it's always nice to have of course.
a

arocnies

10/25/2018, 2:05 PM
Personally, I like to provide API's with suspending functions. If I later need Java interop, I would wrap the suspending functions with an ___Async() function.
👍 1
4 Views