From the Kotlin Koan question(
kotlin-koans/n16FlatMap.kt at master · Kotlin/kotlin-koans · GitHub), I have this Koan code. How do I read this? It looks like a variable with val
, but it is a function with a ()
and {}
.
val Customer.orderedProducts: Set<Product> get() {
// Return all products this customer has ordered
todoCollectionTask()
}
alanfo
2
It’s a read-only extension property of the Customer type called orderedProducts.
It returns a Set<Product>
and has an explicit getter i.e. a function with signature get() to obtain the value of the property.
It’s sometimes easier to read these if the getter - suitably indented - is placed on the next line.
val Customer.orderedProducts: Set<Product>
get() {
// Return all products this customer has ordered
todoCollectionTask()
}