is this creating two vals?
# announcements
j
is this creating two vals?
👌 5
j
Copy code
val (name, age) = person
the above does not make sense.? It states that it is converted to:
Copy code
val name = person.component1()
val age = person.component2()
what is component1() ?
a
Yes, components are automatically declared inside of data classes for each variable, and for list there are 5 predefined.
💯 2
You can also define more for lists yourself, they're just static extensions:
Copy code
fun <T> List<T>.component6() = get(5)  // or this[5]
j
ahh ok so if i have a
Person
data class like this:
Copy code
data class Person(val name: String, val age: Int)
Component1() = name of type string and component2() = age of type Int?
a
Yes, not
Int?
but
Int
.
j
o sorry that ? was a valid question mark related to the whole setnance
sentance*
t
see the automatically derived functions for a data class in the docs https://kotlinlang.org/docs/reference/data-classes.html
`componentN()` functions corresponding to the properties in their order of declaration;
a
The syntax is called destructing declaration by the way if you are wondering
j
Brilliant thanks