Answer the question
In order to leave comments, you need to log in
How to change return type for getter in Kotlin?
class Order(iitems: List<OrderItem>) {
private val items: MutableList<OrderItem> = items.toMutableList()
}
class Order(items: List<OrderItem>) {
private val items: MutableList<OrderItem> = items.toMutableList()
get():List<OrderItem> = field.toList()
}
class Order(items: List<OrderItem>) {
private val _items: MutableList<OrderItem> = items.toMutableList()
val items: List<OrderItem>
get() = _items.toList()
}
Answer the question
In order to leave comments, you need to log in
Normal code. Unless .toList() can be removed.
In Kotlin version 1.4, it will be possible to use delegation by property:
val items: List<OrderItem> by this::_items
The code works, but Kotlin seems to get verbose in this scenario.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question