February 2018
Intermediate to advanced
350 pages
7h 35m
English
Sometimes, we want to reuse values from an existing instance. The copy() method lets us create new instances of a data class, overriding the parameters that we want:
val myItem = Item(myAlmondCupcake, 0.40, 5)val mySecondItem = myItem.copy(product = myCaramelCupcake) //named parameter
In this case, mySecondItem copies unitPrice and quantity from myItem, and replaces the product property.
Read now
Unlock full access