the result is the same String.Companion `Type mism...
# announcements
r
the result is the same String.Companion
Type mismatch: inferred type is String.Companion but String was expected
d
Copy code
inline fun <reified T : Any> NamedParameterJdbcTemplate.queryForKObject(sql: String, parameters: Map<String, *>): T {
    return this.queryForObject(sql, parameters, T::class.java)
}
r
thanks the final code
Copy 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
Why are you casting to T?
r
because
queryForObject
have
@Nullable
so kotlin compiler emit warning
d
Then you should handle the nullable
And not just cast it, then you're back to Java's NPEs
r
but I know for sure that it can not be null, checks are done before the call
d
Then caller of
queryForKObject
should cast, not
queryForKObject
r
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
I guess you can write
!!
instead of
as T
.