https://kotlinlang.org logo
#android
Title
# android
m

Michael Lever

01/22/2020, 8:35 AM
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

Drew Hamilton

01/22/2020, 8:40 AM
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

Michael Lever

01/22/2020, 8:49 AM
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 🙂
3 Views