andrewoma
01/18/2016, 11:41 PMequals
and hashcode
, can we change the generated functions to be less ugly. Here's a generated example:
class Foo(val bar: String, val baz: Int, val quz: String?) {
override fun equals(other: Any?): Boolean{
if (this === other) return true
if (other !is Foo) return false
if (bar != other.bar) return false
if (baz != other.baz) return false
if (quz != other.quz) return false
return true
}
override fun hashCode(): Int{
var result = bar.hashCode()
result += 31 * result + baz
result += 31 * result + (quz?.hashCode() ?: 0)
return result
}
}
And here's something like what I'm after:
class Foo2(val bar: String, val baz: Int, val quz: String?) {
override fun equals(other: Any?) = equals(this, other) { o1, o2 ->
return o1.bar == o2.bar && o1.baz == o2.baz && o1.quz == o2.quz
}
override fun hashCode() = Objects.hash(bar, baz, quz)
}
Using the generic:
inline fun <reified T> equals(t: T, other: Any?, equate: (t1: T, t2: T) -> Boolean): Boolean {
if (t === other) return true
if (other !is T) return false
return equate(t, other)
}