Roman N
Roman N

Reputation: 5126

How to get returned value in generic function?

Please look at code sample:

fun <T> query(sql: String, params: JsonArray): T? {
    val row = otherfun(sql, params) 
    return row.mapTo(T)
}

How to pass in mapTo(Class klz) function (It's java function) proper argument?

Upvotes: 1

Views: 35

Answers (1)

zsmb13
zsmb13

Reputation: 89668

You need to make your function reified (and therefore inline), and then you can use ::class.java to get a Classinstance:

inline fun <reified T> query(sql: String, params: JsonArray): T? {
    val row = otherfun(sql, params)
    return row.mapTo(T::class.java)
}

Upvotes: 3

Related Questions