I am trying to port an existing Swift code to Kotlin and I'd like to use best practice for the following Swift code:
struct Deck {
private(set) var cards: [Card]
var cardsCount: Int {
return self.cards.count
}
init(cards: [Card] = []) {
self.cards = cards
}
mutating func add(card: Card) {
self.cards.append(card)
}
}
The design goals are:
cardsproperty is not modifiable outside of the class so its type should beList<Card>fun add(card: Card)should modify the internalcardslist
Is there a way to achieve this in Kotlin without using two separate properties - one private var mutableCards: MutableList<Card> and one computed property val cards: List<Card> get() = this.mutableCards
I need some best practice for such situation.