Is it possible to simplify this code?


#1

I have a java interface where each method has a callback:

interface AccountService {
    fun create(account: Account, handler: Handler<AsyncResult<Long>>)
    operator fun get(id: Long, handler: Handler<AsyncResult<Account>>)
}

Than I want to use this methods as coroutine, I use this code:

suspend fun <T : Any?> await(f: (Handler<AsyncResult<T>>) -> Unit) : T {
    return awaitResult<T> {
        f(it)
    }
}
await<Account> { accountService.get(1, it) }

Is it possible to call methods in more beautiful way?

await<Account>(accountService.get(1))
// or even
await<Account> accountService.get(1)

I see only one possible solution to create method extension like xyzAwait() and wrap methods with callbacks


#2

I made a proposal about this