19 Extensions
Extensions allow you to add functionality to a type without directly modifying the type’s definition. You can use extensions with your own types and also types you do not control, like List, String, and other types from the Kotlin standard library.
Extensions are an alternative to the sharing behavior of inheritance.
They are a good fit for adding functionality to a type when you do not control the definition of the class or when a class is not marked with the open keyword and therefore is ineligible for subclassing.
The Kotlin standard library frequently uses extensions. For example, the scope functions that you learned about in Chapter 12 are defined as extensions, and you will look at some examples of their declarations ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access