August 2017
Intermediate to advanced
440 pages
10h
English
In this section, we will first understand what extension properties are, and then we will move on to learn where these properties can be used. As we already know, properties in Kotlin are defined by their accessors (getter and setter):
class User(val name: String, val surname: String) {
val fullName: String
get() = "$name $surname"
}
We can also define the extension property. The only limitation is that this property can't have a backing field. The reason for this is that extension can't store state, so there is no good place to store this field. Here is an example of an extension property definition for TextView:
val TextView.trimmedText: String
get() = text.toString().trim()
// Usage
textView.trimmedText
As with ...
Read now
Unlock full access