Seokjae Lee
04/26/2024, 2:43 AM// Java
public class TestRepository {
public <T> List<T> getCollectionWhichHasNull() {
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().getCollectionWhichHasNull()
// 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 🙂ephemient
04/26/2024, 3:16 AMNullable
or NotNull
annotations) as having "platform types", or unknown nullability. it'll let you treat them as either nullable or non-nullable without warning, and if you assume they're non-nullable you get the same potential for NullPointerException
as you would in Java.Seokjae Lee
04/26/2024, 4:16 AM@NotNull
.
Thanks for your explanation with document thank you color