I have a class which implement `MutableList<T&g...
# announcements
p
I have a class which implement
MutableList<T>
with a delegate. The class doesn’t have other properties. How can I write a working
equals
method which includes the delegate. Can I access it somehow?
Copy code
class WidgetListBuilder : MutableList<Widget> by mutableListOf() {

    inline fun <T : Widget> add(widget: T, init: T.() -> Unit): T {
        widget.apply(init)
        add(widget)
        return widget
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is WidgetListBuilder) return false
        // TODO handle list
        return true
    }

    override fun hashCode(): Int {
        return javaClass.hashCode()
    }
}
d
This should work:
Copy code
class WidgetListBuilder private constructor(val delegate: MutableList<Widget>) : MutableList<Widget> by delegate {
    constructor() : this(mutableListOf())
}
p
Thanks!
adding
data class
makes it even better!
Alternatively I could use a typealias and a extension function for
add
to reduce the class overhead
d
Probably a better case for the upcoming inline classes