What are extension properties in Kotlin?
Extension properties in Kotlin are properties that are added to existing classes without modifying their source code. They enable you to augment the functionality of classes, including both standard library classes and user-defined classes, in a concise and flexible manner.
Extension properties are declared in a similar way to extension functions, but they do not have a backing field. Instead, you must provide custom getters and setters for the property.
Here’s an example of an extension property:
kotlin val String.hasEvenLength: Boolean get() = this.length % 2 == 0
In this example, hasEvenLength is an extension property defined for the String class. It provides a custom getter that checks whether the length of the string is even.
Extension properties offer a convenient way to add new properties to existing classes, enhancing their functionality and enabling more expressive code.