DEV Community

Discussion on: Nulls and null checks - How to work safely with nulls in any codebase

Collapse
 
nfrankel profile image
Nicolas Frankel
val canBeNull: String? = null
val cannotBeNull: String = "Foo"
Enter fullscreen mode Exit fullscreen mode

In the above code, the compiler will fail if you if you try to use canBeNull without applying null checks first. In the second case, it won't because... well, it cannot be null.

Note that I explicitly set the type on the second variable though it's not necessary for better readability.

Thread Thread
 
sargalias profile image
Spyros Argalias

Thanks for the clarification. I modified that section to mention non-nullable types and credited you for pointing them out.

Please let me know if you don't want the credit or if you'd rather be credited in a different way.

Thread Thread
 
nfrankel profile image
Nicolas Frankel

I didn't expect credit so that's very fine, thanks!