Hey I'm learning Kotlin using the Kotlin_Koans IDE...
# android
m
Hey I'm learning Kotlin using the Kotlin_Koans IDE course in Android Studio. I've just done a task about Data Classes but I'm so confused by what this class is supposed to be doing. It's comparing two names that are the same, despite having two different names in the list, why? I'll copy the correct answer below, then underneath I'll paste the 'task'. data class Person(val name: String, val age: Int) fun getPeople(): List<Person> { return listOf(Person("Alice", 29), Person("Bob", 31)) } fun comparePeople(): Boolean { val p1 = Person("Alice", 29) val p2 = Person("Alice", 29) return p1 == p2 // should be true }
d
The data class declaration is the solution for rewriting the same class as a Kotlin data class, demonstrating that it is shorter to write. I’m not sure what the
getPeople
function is for; it’s unused. The
comparePeople
function demonstrates that
equals
works properly for the data class, even though you didn’t write an
equals
function
m
Fortunately I understood the data class declaration bit, I was just so confused by the unused getPeople function and I'm not sure what real world application there is of the comparePeople function either.
Thank you for the reply 🙂