2 ms·
The author wrote a lot about nullable types, but didn't mention a very useful feature of the Kotlin compiler: it understands several (IIRC, more than a dozen) t
by cesarb 8y ago
The author wrote a lot about nullable types, but didn't mention a very useful feature of the Kotlin compiler: it understands several (IIRC, more than a dozen) third-party Java nullability annotations. It treats Java types annotated with @NotNull or equivalent as T, types annotated with @Nullable as T?, and only types with neither are treated as T!.
And on the other direction, the bytecode generated by the Kotlin compiler also has these Java nullability annotations (it uses the org.jetbrains annotations package), so for instance Dagger2 can know that a type is nullable from the presence of the @Nullable annotation.
If your code already uses @NotNull/@Nullable everywhere, introducing Kotlin to your code base becomes easier. As a bonus, IDEs can sometimes use these to warn you of potential null pointer mistakes in your code.