https://kotlinlang.org logo
#announcements
Title
# announcements
x

xenoterracide

06/13/2019, 7:28 PM
I feel like I’ve seen code like this written
Copy code
deserialize<PatientDto>(message.payload)
where the called method then gets the class
Copy code
internal fun <DTO : Any> deserialize( ba: ByteArray, clazz: KClass<DTO> ): DTO {
        return mapper.readValue(ba, clazz.java)
    }
pretty sure it’s in gradle’s kotlin DSL somewhere… how do I replicate that calling signature?
c

Casey Brooks

06/13/2019, 7:37 PM
p

Pavlo Liapota

06/13/2019, 7:38 PM
Something like this:
Copy code
inline fun <reified T> foo() {
    foo(T::class)
}

fun <T> foo(clazz: KClass<T>) {
    
}
x

xenoterracide

06/13/2019, 7:43 PM
does it have to be only that as a parameter?
Copy code
internal inline fun <reified DTO : Any> deserialize(ba: ByteArray, clazz: Class<DTO> ): DTO {
        return mapper.readValue(ba, clazz)
    }
isn’t showing me any love
the caller says the parameter is missing
c

Czar

06/13/2019, 8:20 PM
you've added an unnecessary param to the signature. Should be:
Copy code
internal inline fun <reified DTO : Any> deserialize(ba: ByteArray): DTO {
    return mapper.readValue(ba, DTO::class.java)
}
💯 3
2 Views