Johann Pardanaud
08/17/2024, 10:31 PMWrappedSet
class delegating to an internal Set
. This allows me to provide all the features of a Set, and also provide additional methods without polluting the extension functions of the Set
interface.
data class WrappedSet(private val items: Set<Int>) : Set<Int> by items {
fun someCustomMethod(): Unit {}
}
However, this simple test shows I broke the symmetric requirements of the `equals` method:
val pureSet = setOf(1)
val wrappedSet = WrappedSet(setOf(1))
println(pureSet == wrappedSet) // true
println(wrappedSet == pureSet) // false
What are your recommandations here? Should I provide a custom equals implementation that allows WrappedSet
to be compared to a Set
interface?Youssef Shoaib [MOD]
08/17/2024, 10:36 PMJohann Pardanaud
08/17/2024, 10:50 PMWrappedSet
will be considered equal to a Set
, if they have the same elements, is that OK?ephemient
08/18/2024, 12:25 AMJohann Pardanaud
08/18/2024, 8:01 AMMichael de Kaste
08/19/2024, 3:06 PMclass WrappedSet(
items: Set<Int>,
) : Set<Int> by items {
override fun equals(other: Any?) = (this as Set<*>) == other
override fun hashCode(): Int = (this as Set<*>).hashCode()
}
Johann Pardanaud
08/19/2024, 5:37 PMclass WrappedSet(private val items: Set<Int>) : Set<Int> by items {
override fun equals(other: Any?) = items == other
override fun hashCode(): Int = items.hashCode()
}