cueball
09/24/2020, 6:22 PMreactive.function.client
method awaitBody()
from a Generic class and am running into error:
Kotlin: Cannot use 'Response' as reified type parameter. Use a class instead.
---
class ReactiveWebClient<Request: Any, Response: Any> {
suspend fun post(uri: URI, request: Request, headers: (HttpHeaders) -> Unit): Response {
return <http://webClient.post|webClient.post>()
.uri(uri)
.body(BodyInserters.fromValue(request))
.headers(headers)
.retrieve()
.awaitBody() // error on this line
}
}
Signature of `awaitBody`:
public suspend inline fun <reified T : kotlin.Any> org.springframework.web.reactive.function.client.WebClient.ResponseSpec.awaitBody(): T { /* compiled code */ }
Can someone please help? I’m not so well versed with Kotlin’s type parameters.Dennis
09/25/2020, 7:32 AMawaitBody
but at the call site (inside your class) the type is not known at compilation time (due to type erasure)
I'm actually not sure how you solve this.
How about extracting post
into an extension method that uses a reified type parameter and can be inlined? The webClient you're wrapping could be the target?uli
09/25/2020, 9:55 AMclass ReactiveWebClient<Request: Any, Response: Any> {
suspend inline fun <reified T: Response> post(uri: URI, request: Request, headers: (HttpHeaders) -> Unit): T
suspend inline fun <reified Response: Any> post(uri: URI, request: Request, headers: (HttpHeaders) -> Unit): Response
cueball
09/28/2020, 9:55 AM