Benoît Quenaudon
1 min readFeb 28, 2019

--

Would you be able to develop a bit on how to design APIs for context requiring instance members?

We wanna hook two channels for bidirectional communication over IO, let the consumer to send/receive events. The API could look something like: fun routeChat(context: CoroutineContext): Pair<SendChannel<RequestObject>, ReceiveChannel<ResponseObject>>

We’re not really happy with requiring the context as an argument. Another approach we’re taking is, pulling the coroutineContext intrinsic property instead of requiring an argument, by making the method suspend:suspend fun routeChat(): Pair<SendChannel<RequestObject>, ReceiveChannel<ResponseObject>>

Now, it doesn’t really feel right either because the method is synchronous.

What design pattern would you suggest in this case? I repeat that this is a instance’s member method.

--

--