Lambda 简化kotlin方法调用
请看代码:Lambda 简化kotlin方法调用,lambda,kotlin,Lambda,Kotlin,请看代码: suspend fun <T : Any?> await(f: (Handler<AsyncResult<T>>) -> Unit) : T { return awaitResult<T> { f(it) } } await<Account> { accountService.get(1, it) } // (*) suspend-fun-await(f:(处理程序)->Uni
suspend fun <T : Any?> await(f: (Handler<AsyncResult<T>>) -> Unit) : T {
return awaitResult<T> {
f(it)
}
}
await<Account> { accountService.get(1, it) } // (*)
suspend-fun-await(f:(处理程序)->Unit):T{
返回等待结果{
f(it)
}
}
等待{accountService.get(1,it)}/(*)
是否可以用这种方式重写(*)行(我想消除难看的“it”语法):
wait(accountService.get(1))
//或者只是
等待accountService。获取(1)
是不是帐户(1)
应该等于{accountService.get(1,it)}
?是的,我将更新我的主题以澄清我的猜测,您必须编写另一个函数来包装等待调用,类似于等待帐户(Int)
。您无法完全摆脱accountService.get(n,it)
,因为它是异步方法的主体。我对协同程序的看似落后的使用感到困惑。当corroutines和suspend fun
s被发明来消除时,你究竟为什么要将回调传递给await
。但是我想找到一个比为每个接口创建扩展方法更好的解决方案,因为它看起来是多余的
await<Account>(accountService.get(1))
// or just
await<Account> accountService.get(1)