【发布时间】:2019-06-19 07:32:00
【问题描述】:
我在 Single class 上有扩展功能:
fun <T: Response<BaseResponse>> Single<T>.checkResponse(): Single<BaseResponse> {
return this.map { some code }
}
并尝试像这样使用它:
fun sign(body: Map<String, String>): Single<SignResponse> {
return service.sign(body).checkResponse().map { it }
}
服务的sign签名:
@POST(Urls.SIGN)
fun sign(@Body body: Map<String, String>): Single<Response<SignResponse>>
SignResponse 显然继承了 BaseResponse
响应是 retrofit2.response
我有一个错误:
Type parameter bound for T in fun <T : Response<BaseResponse>> Single<T>.checkResponse(): Single<BaseResponse> is not satisfied: inferred type Response<SignResponse> is not a subtype of Response<BaseResponse>
如何编写扩展函数来告诉编译器 Single 有 Response,其类型继承自 BaseResponse?
【问题讨论】:
标签: generics kotlin kotlin-extension