August 2017
Intermediate to advanced
440 pages
10h
English
By default, the Kotlin compiler will also generate an appropriate copy method that will allow us to easily create a copy of an object:
data class Product(var name: String, var price: Double)
val productA = Product("Spoon", 30.2)
print(productA) // prints: Product(name=Spoon, price=30.2)
val productB = productA.copy()
print(productB) // prints: Product(name=Spoon, price=30.2)
Java does not have named argument syntax, so when calling the copy method in Java, we need to pass all arguments (the order of the arguments corresponds to the order of properties defined in the primary constructor). In Kotlin, this approach decreases the need for copy constructors or copy factories:
Read now
Unlock full access