https://kotlinlang.org logo
Title
a

alex

11/06/2019, 11:07 PM
Right now I'm getting
ClassCastException
and debugging shows that
line no. 12
does nothing. What might be the reason, that
inline
version sometimes (for some calls) skips the cast and returns the same object (i.e.
String
) instead of
reified T
? Classic approach with passing class ref works fine.
Callsite looks like this:
fun getModel(id: Int: CompletableFuture<Model> {
        val request: HttpRequest = //...
        return sendAsync(request)
    }
q

Quy D X Nguyen

11/07/2019, 3:33 AM
What's your inline declaration?
a

alex

11/07/2019, 8:33 AM
@Quy D X Nguyen can you specify what's that? There is an inline function in code snippet, also I added call-site usage.
q

Quy D X Nguyen

11/07/2019, 1:40 PM
Maybe a bug in the readValue method?
a

alex

11/07/2019, 3:28 PM
I guess that problem was that I have a generic over generic type parameter and reified cannot handle this correctly. Solution I came up with:
protected inline fun <reified T> sendAsync(
        request: HttpRequest,
        typeReference: TypeReference<T> = jacksonTypeRef()
    ): CompletableFuture<T> {
        return client.sendAsync(request) { _ ->
            BodySubscribers.mapping(BodySubscribers.ofString(StandardCharsets.UTF_8)) {
                mapper.readValue(it, typeReference)
            }
        }.thenApply { it.body() }
    }
looks the same, but instead of simple
reified T
, I'm handing over
TypeReference
as well.
It pretty much looks like this: https://youtrack.jetbrains.com/issue/KT-22798 , so I think it's a bug?