<@U0KBF8D7V> Helper methods to generate these for ...
# announcements
c
@Paul Woitaschek Helper methods to generate these for you would require reflection, and
equals()
and
hashCode()
are not methods where you want to do that. Guava has some helper methods, though: https://gist.github.com/rocketraman/1399080/a50ea2206f6ea4bda65ef00d1f54aab6d654d3bd
đź‘Ť 2
p
Why does it need reflection? That compareTo has helpers too where you can specify a comparision chain
I was thinking about sth like
Copy code
fun hashCodeOf(vararg elements: Any?): Int {
    var hash = 17
    elements.forEach {
      hash = hash * 31 + (it?.hashCode() ?: 0)
    }
    return hash
  }
c
@Paul Woitaschek A method that would be able to generically compute hashcode/equals would need to reflect on your class. Guava picked the better approach of offering helper methods you’d use inside your own implementation of equals/hashCode
p
My function is made to be used inside my own implementation of hashcode.