Extensions on Nullable Types and lateinit Property
Explore Kotlin's approach to nullability by understanding special extension functions like orEmpty and isNullOrEmpty that safely handle nullable variables. Learn how to use the lateinit property to declare non-nullable properties without immediate initialization, enhancing code safety and flexibility.
We'll cover the following...
Nullable types and extensions
Regular functions cannot be called on nullable variables. However, there is a special kind of function that can be defined such that it can be called on nullable variables. Kotlin stdlib defines the following functions that can be called on String?:
orEmpty: It returns the value if it is notnull. Otherwise, it returns an empty string.isNullOrEmpty: It returnstrueif the value isnullor empty. Otherwise, it returnsfalse.isNullOrBlank: It returnstrue...