【发布时间】:2020-08-09 21:36:34
【问题描述】:
我正在尝试使用 Retrofit 的响应创建一个内联函数,但是当我按下 . 并尝试调用该函数不可见时,我尝试不调用它并且它是可见的。
我有这个
@GET("url")
suspend fun getModel(): Response<ModelEntity>
我的内联函数是
inline fun <T, R> safeCall(
block: () -> Response<T>,
transform: (T) -> R,
errorFactory: FailureFactory = FailureFactory()
): Either<ErrorEntity, R> =
try {
val result = block()
when (result.isSuccessful) {
true -> Either.Right(transform(result.body()!!))
false -> Either.Left(errorFactory.handleCode(result.code()))
}
} catch (exception: IOException) {
Either.Left(errorFactory.handleException(exception))
}
但我不能调用类似的东西
service.myList().safeCall(...)
即使它返回 Response<T>
我的想法是这样称呼它
override fun getMessages() =
service.getMessages()
.safeCall(transform = { it.map { msgs-> msgs.toDomain() }})
}
【问题讨论】:
标签: android generics kotlin retrofit