I've got Java Code that looks like this (Spring's ...
# getting-started
b
I've got Java Code that looks like this (Spring's ResponseEntity<T>)
Copy code
class Container<T> {
    @Nullable
    private T value;

    public Container(@Nullable T value) {
        this.value = value;
    }

    @Nullable
    public T getValue() {
        return value;
    }
}
Obviously, creating a Container<String> in Kotlin will still force the container.value return type to be nullable; is there a nice way around that? I've currently resorted to:
Copy code
val <T> ResponseEntity<T>.nonNullBody: T
    get() = body!!
s
Maybe you could subclass it? Bit messy, but it should work.
Copy code
class MyContainer<T>(private val value: T): Container<T>(value) {
  override fun getValue(): T = value
}
b
yeah, problem is that I don't have access to where it's created
container is being returned from Spring Proxies built out of Interfaces
s
I don't think you can fix it then. The problem is basically that the nullability annotations in the Spring source code are wrong (or at least, imprecise). IMO the generic parameter should be annotated nullable and the other methods should have no annotations.
b
would basically need to wrap all proxy methods
I see, thank you
looking at org.springframework.lang.Nullable or the jakarta equivalent, none can be applied to type parameters
😢 1
s
I've not used nullability annotations much but I'm wondering if they're really needed at all in that scenario. I'm sure most type systems that understand nullability would get it right without the annotations. Adding the annotations serves no purpose but to mistakenly imply that
getValue
can return
null
even when
T
was originally non-nullable 😢
b
no, not how that works
if you don't annotate it, it will return a platform type
as in: Container<String> will return String! instead of String for the getter
not even making the property final will do that
ok, found the solution
jetbrains annotations were pulled in in version 13.x.x, you can add those annotations in newer versions to produce that result
unfortunately does not fix Spring's usage
a
What’s the problem with it being a platform type? You can use it like a non-nullable type
b
@asdf asdf I think Spring went out of it's way to add nullability information to give you a better Kotlin experience
platform types are basically "can explode" types
so you aren't sure if they're null or not