https://kotlinlang.org logo
Title
r

rrader

08/07/2018, 1:53 PM
the result is the same String.Companion
Type mismatch: inferred type is String.Companion but String was expected
d

diesieben07

08/07/2018, 1:54 PM
inline fun <reified T : Any> NamedParameterJdbcTemplate.queryForKObject(sql: String, parameters: Map<String, *>): T {
    return this.queryForObject(sql, parameters, T::class.java)
}
r

rrader

08/07/2018, 1:59 PM
thanks the final code
inline fun <reified T : Any?> NamedParameterJdbcTemplate.queryForKObject(sql: String, parameters: Map<String, *>): T {
    return this.queryForObject(sql, parameters, T::class.java) as T
}
and the call
jdbcTemplate.queryForKObject<String>(sql, parameters)
d

diesieben07

08/07/2018, 1:59 PM
Why are you casting to T?
r

rrader

08/07/2018, 2:00 PM
because
queryForObject
have
@Nullable
so kotlin compiler emit warning
d

diesieben07

08/07/2018, 2:00 PM
Then you should handle the nullable
And not just cast it, then you're back to Java's NPEs
r

rrader

08/07/2018, 2:01 PM
but I know for sure that it can not be null, checks are done before the call
d

diesieben07

08/07/2018, 2:01 PM
Then caller of
queryForKObject
should cast, not
queryForKObject
r

rrader

08/07/2018, 2:04 PM
queryForKObject
is just a wrapper to not have long calls now it is
jdbcTemplate.queryForKObject<String>(sql, parameters)
, was
jdbcTemplate.queryForObject<String>(sql, parameters, String::class.java) as String
I need to duplicate type twice in old version
p

Pavlo Liapota

08/07/2018, 2:19 PM
I guess you can write
!!
instead of
as T
.