I have a nullability question: this works but Kotl...
# getting-started
s
I have a nullability question: this works but Kotlin thinks that the return value could be "T" instead of "T?".
Copy code
inline fun <reified T : Any> RestTemplate.getForObject(path: String): T? {
    return this.getForObject(path, T::class.java)
}
but the RestTemplate.getForObject is a nullable Java function:
Copy code
@Nullable
    public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException {
Is this a type inference bug/confusion?
s
It works as expected for me — I get a warning if I try to return
T
, and no warning if I follow the suggestion to change it to
T?
. What versions of Spring/Kotlin/IntelliJ are you using?
s
good to hear it works as expected on your machine! Spring: 2.7.12 Kotlin: 1.9.0 JVM: 17 IntelliJ: 2023.2 Build #IU-232.8660.185, built on July 26, 2023
x
A generic type
T
can be nullable in Kotlin. Using
T?
is redundant, so I can figure that's the reason for the warning
m
that would be true, but T has been specified to be T : Any
s
Even if
T
isn’t specified as non-null,
T
and
T?
are still different. Unbounded
T
is maybe-nullable, and
T?
is definitely-nullable.
x
Oh you're right. I wasn't seeing that upper
Any
bound
m
@Stephan Schröder I have the same as @Sam, perhaps some issues with intellijs caching and whatnot, try clearing them
s
this morning there was an IntelliJ update to 2023.2.1 and now the wrong warning is gone 🙂
🎉 1
👏 1