Extension properties
Kotlin also offers adding properties to existing types with extension properties. Just as extension functions have the type they are extending before a function name, extension properties are like normal properties with the addition of the extended type before the property name. The function we wrote in the previous section can also be written as a property:
val Int.isPowerOf2: Boolean get() = this > 0 && ((this and this - 1) == 0)
Now, we can access this property on any variable of Int
type:
val n = 16 val isPowerOf2 = n.isPowerOf2
You can also have a mutable extension property, where you'd have to implement the set
method also.