Hi all! I have a question about reified function calls from abstract classes:
Copy code
abstract class BaseClient<T> constructor(private val httpClient: HttpClient) {
suspend fun get(): T = httpClient.get<T>("/api/") // T is a reified type: HttpClient.get<reified T>(path: String)
}
Rightly, it will not work and Intellij gives me a warning: Use a class instead! I have the issue, that I cannot pass a class as the httpclient doesn't support that (at least out of the box)
Is there any way I can make this work? Or add some kind of 1 line code I should add/override in the child class that extends from BaseClient?
t
turansky
03/08/2021, 6:08 PM
What about using
KClass
?
Copy code
val dataClass: KClass<T> = T::class
suspend fun get(): T =
client.get("/api/", dataClass)
j
Joost Klitsie
03/08/2021, 6:13 PM
Does that method exist in the ktor library? I couldn't find it :(
t
turansky
03/08/2021, 7:59 PM
Screen Shot 2021-03-08 at 10.59.16 PM.png
turansky
03/08/2021, 8:00 PM
You can receive
HttpResponse
turansky
03/08/2021, 8:01 PM
And then use it
call
TypeInfo
can be constructed from
KClass
j
Joost Klitsie
03/08/2021, 8:18 PM
Thanks for the effort! I might just copy/paste the builder methods and then indeed add the kclass parameter, but it would be annoying to maintain :) I made now some easy pattern that shouldn't require too much code in the implementing classes