Kotlin lets you add new members to any class with the extensions mechanism. Namely, there are two types of extensions: extension functions and extension properties. They look a lot like normal functions and properties but with one important difference: you need to specify the type that you extend.
data class Item(val name: String, val price: Float) // 1
data class Order(val items: Collection<Item>)
fun Order.maxPricedItemValue(): Float = this.items.maxByOrNull { it.price }?.price ?: 0F // 2
fun Order.maxPricedItemName() = this.items.maxByOrNull { it.price }?.name ?: "NO_PRODUCTS"
val Order.commaDelimitedItemNames: String // 3
get() = items.map { it.name }.joinToString()
fun main() {
val order = Order(listOf(Item("Bread", 25.0F), Item("Wine", 29.0F), Item("Water", 12.0F)))
println("Max priced item name: ${order.maxPricedItemName()}") // 4
println("Max priced item value: ${order.maxPricedItemValue()}")
println("Items: ${order.commaDelimitedItemNames}") // 5
}
- Defines simple models of
Item
andOrder
.Order
can contain a collection ofItem
objects. - Adds extension functions for the
Order
type. - Adds an extension property for the
Order
type. - Calls extension functions directly on an instance of
Order
. - Accesses the extension property on an instance of
Order
.
It is even possible to execute extensions on null
references. In an extension function, you can check the object for null
and use the result in your code:
//sampleStart
fun <T> T?.nullSafeToString() = this?.toString() ?: "NULL" // 1
fun main() {
println(null.nullSafeToString())
println("Kotlin".nullSafeToString())
}
//sampleEnd