October 2019
Intermediate to advanced
434 pages
11h 54m
English
Data classes will generate component functions for all primary constructor properties. For each primary constructor property, a componentX function will be created where X is the order of the property. We can illustrate how this works using the following Article class:
data class Article(var title: String, val author: String) { var snippet: String = ""}
For our Article example, the compiler will generate component1() and component2() methods:
public final class Article { ... @NotNull public final String component1() { return this.title; } @NotNull public final String component2() { return this.author; }}
These component functions can then be used by the compiler to support destructuring declarations, shown as follows:
fun Read now
Unlock full access