rook
10/27/2017, 4:20 PM.equals()
call, but not a ==
callAndreas Sinz
10/27/2017, 8:35 PMequals()
per class, how would you implement it with sealed classes?bdawg.io
10/28/2017, 7:05 AM==
only works on the equals
operator. You can’t overload it, but rather only override it (from Any
).
override fun equals(other: Any?): Boolean {
return if (other is Color) {
name == other.name
} else if (other is String) {
name == other
} else {
super.equals(other)
}
}
but just be sure to fulfill the hashCode
contract as wellbdawg.io
10/28/2017, 7:06 AMoverride fun hashCode(): Int = name.hashCode()
bdawg.io
10/28/2017, 7:07 AMNote that theoperator in Kotlin code is translated into a call to [equals] when objects on both sides of the operator are not null.==