Daniel
03/25/2020, 10:15 PMfun <T> List<T>.isSameAs(other: List<T>): Boolean {
if(size != other.size) return false
forEachIndexed { index, element ->
if(element !== other[index]) return false
}
return true
}
Does anybody have an idea? Also maybe there might be a better way to check this?Shawn
03/25/2020, 10:31 PMhashCode() isn’t good enough for equality. You might consider adding an if (this === other) return true but it’s not strictly necessaryShawn
03/25/2020, 10:31 PMidenticalTo()Shawn
03/25/2020, 10:33 PMforEach/forEachIndexed for side-effect-ey operations that aren’t at the end of a call chain but that’s a nit more than anything in this casebitnot
03/26/2020, 9:55 AMcontentEquals?
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/content-equals.htmlMike
03/26/2020, 12:33 PMcontentEquals is for Array, not List. Otherwise, I think they're the same, and you have just provided a meaningful name.Shawn
03/26/2020, 2:55 PMcontentEquals compares with `==/.equals()`, the requirement was identity equality of the elements ===Shawn
03/26/2020, 4:13 PMinfix fun <E> List<E>.identicalTo(other: List<E>): Boolean = when {
this === other -> true
size != other.size -> false
else -> withIndex().all { (index, value) ->
value === other[index]
}
}
or this very mildly cursed looking thing
infix fun <E> List<E>.identicalTo(other: List<E>): Boolean =
other === this
|| other.size == size
&& withIndex().all { (index, value) ->
value == other[index]
}Daniel
03/26/2020, 5:58 PMDaniel
03/26/2020, 5:59 PM