0

I have extension function on Single class:

fun <T: Response<BaseResponse>> Single<T>.checkResponse(): Single<BaseResponse> {
    return this.map { some code }
}

And trying to use it like:

fun sign(body: Map<String, String>): Single<SignResponse> {
    return service.sign(body).checkResponse().map { it }
}

Service's sign signature:

@POST(Urls.SIGN)
fun sign(@Body body: Map<String, String>): Single<Response<SignResponse>>

SignResponse inherits BaseResponse obviously

Response is retrofit2.response

And I've got an error:

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>

How do I need to write extension function to tell compiler that Single have Response, which have type inherited from BaseResponse?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

You can try something like this:

fun <R: BaseResponse, T: Response<R>> Single<T>.checkResponse(): Single<R>
gpunto
  • 2,573
  • 1
  • 14
  • 17