如何使用嵌套泛型编写扩展函数?
How to write extension function with nested generics?
我在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 }
}
服务签名:
@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?
您可以尝试这样的操作:
fun <R: BaseResponse, T: Response<R>> Single<T>.checkResponse(): Single<R>
我在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 }
}
服务签名:
@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?
您可以尝试这样的操作:
fun <R: BaseResponse, T: Response<R>> Single<T>.checkResponse(): Single<R>