Seokjae Lee
04/26/2024, 2:40 AM// Java
public class TestRepository {
public <T> List<T> getNullId() {
return Collections.singletonList(null);
}
}
// Kotlin
fun main() {
// type is List<String>, which means that this collection has non-null value (I think...)
// There's any compilation error is occurred, why?
val result: List<String> = TestRepository().getNullId()
// Although result variable's type is List<String>, it contains null item...
for (s in result) {
println(s != null) // false
}
}
If you have any references or blog posts, I'd appreciate it 🙂Seokjae Lee
04/26/2024, 2:40 AMjw
04/26/2024, 2:41 AMSeokjae Lee
04/26/2024, 2:42 AMjw
04/26/2024, 2:43 AMSeokjae Lee
04/26/2024, 2:58 AMkevin.cianfarini
04/28/2024, 6:05 PMSomeType!
and nullability enforcement from the kotlin side doesn't apply.
Unless the Java interface is annotated with something like @NonNull
or @Nullable
I usually try to hedge against these problems by always treating the types as nullable.kevin.cianfarini
04/28/2024, 6:10 PMList<String!>
but it would be safer to treat it as List<String?>