Kotlin Data class.
fun main() {
val emp = Employee("John", true) // Regular class instantiation
println(emp)
val car = Car("blue", "Toyota", 2015) // Data class instantiation
println(car) // Improved toString(), shows all labels and values, instead of reference
val emp2 = Employee("John", true)
println(emp == emp2) // false as are referentially inequal
val car2 = Car("blue", "Toyota", 2015)
println(car == car2) // Compares structurally instead of referentially. Uses equals() function
val car3 = car.copy() // Makes a structurally identical object
println(car3)
val car4 = car.copy(model = "Audi") // Copy with variations
println(car4)
val car5 = car.copy("Silver", "Audi", 2004) // Copy with variations. Can exclude identifier if changing all, but then why would you copy?
println(car5)
}
// Main purpose of data class is to store state
// Data classes have improved toString() function, custom implementation of the equals() and hashcode() functions and have copy() function. All can be overridden
// Have all that is needed for a destructuring declaration
// Cannot be abstract, sealed or inner classes
data class Car(val colour: String, val model: String, val year: Int) { // Must have at least one parameter in constructor, all must have val or var i.e. must be declared in constructor
val notMe: String = "nope" // will not benefit from improved functions as not declared in constructor
}
//Regular class for comparison
class Employee (val firstName: String, val fullTime: Boolean)
