https://kotlinlang.org logo
#ktor
Title
# ktor
r

ribesg

03/04/2019, 3:37 PM
Hey, how do I use (non-
reified
) generics with the ktor client?
This obviously doesn’t work
Copy code
suspend fun <T> get(url: String): T =
        client.get(url)
e

e5l

03/05/2019, 7:35 AM
Hi @ribesg, what kind of
T
do you want to use?
r

ribesg

03/05/2019, 9:00 AM
I’m using
kotlinx.serialization
, so I guess any data class with the annotation?
e

e5l

03/05/2019, 9:03 AM
Could you try
Copy code
suspend fun <T> get(url: String): T =  
        client.get<T>(url)
?
r

ribesg

03/05/2019, 9:13 AM
Doesn’t change anything, it suggests removing it and I still get
Cannot use 'T' as reified type parameter. Use a class instead.
👀 1
Note that IDEA doesn’t say anything, it’s at compilation time that I get this error (which is weird)
e

e5l

03/05/2019, 9:25 AM
It's a bug in IDE: https://youtrack.jetbrains.com/issue/KT-30265. You have to specify
T
explicitly when the parameter can't be reified cause of type erasure.
r

ribesg

03/05/2019, 9:29 AM
Yes I know that but that doesn’t really help, it means you can’t wrap the ktor client in any class/object. I have to change my plans accordingly. I think I understand why it should not work.
e

e5l

03/05/2019, 9:31 AM
You could use
inline
methods with
reified
with
get
like methods. Also you could use
call
or
get<HttpResponse>
explicitly.
Could you provide your project configuration in ticket https://youtrack.jetbrains.com/issue/KT-30265#comment=27-3322624 ?
3 Views