Extension Properties

Just as functions can be extension functions, properties can be extension properties.

The receiver type specification for extension properties is similar to the syntax for extension functions—the type comes right before the function or property name:

fun ReceiverType.extensionFunction() { ... } val ReceiverType.extensionProperty: PropType get() { ... }

An extension property requires a custom getter. Since extensions don’t belong to the class they extend, they don’t have the necessary access to store the value of the extension property—thus there can be no set(). The property value must be computed during each access:

End of sample. See AtomicKotlin.com for full early-access book.



Previous          Next

©2018 Mindview LLC. All Rights Reserved.