Search⌘ K
AI Features

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.

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 not null. Otherwise, it returns an empty string.

  • isNullOrEmpty: It returns true if the value is null or empty. Otherwise, it returns false.

  • isNullOrBlank: It returns true ...