October 2019
Intermediate to advanced
434 pages
11h 54m
English
Data classes also provide us with a copy() method, which can be used to copy a data class. Additionally, we can use named arguments to modify specific properties on the copied class. In this example, we create an instance of Article named article1 and then copy it into article2. During the copy, we change the title property or article2:
fun main(args: Array<String>) { val article1 = Article("Kotlin is great", "Nate").apply { snippet = "an article about Kotlin" } val article2 = article1.copy(title = "Kotlin is great part 2")}
After this copy is completed, the two variables will have different titles, but the author and snippet will be the same.
Code generated by data classes is one of the most convenient and useful parts of the Kotlin ...
Read now
Unlock full access