passsy
08/27/2018, 7:19 PMMutableList<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?
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()
}
}
diesieben07
08/27/2018, 7:25 PMclass WidgetListBuilder private constructor(val delegate: MutableList<Widget>) : MutableList<Widget> by delegate {
constructor() : this(mutableListOf())
}
passsy
08/27/2018, 7:27 PMdata class
makes it even better!add
to reduce the class overheaddiesieben07
08/27/2018, 7:29 PM