David Smith
04/28/2021, 12:42 AMhashCode() already but I want to avoid having to call it as a function because during a diff of a deeply nested structure this could happen many many times, i.e. I want to memoize the call. Can I simply add a property that is initialized to this.hashCode()? I’ve implemented the following which seems to work (although I haven’t tested it properly yet)
sealed class Node {
data class TextNode(val value: String) : Node() {
override val hash = this.hashCode()
}
data class TagNode(val name: String, val attributes: List<Attribute>, val children: List<Node>) : Node() {
override val hash = this.hashCode()
}
abstract val hash: Int
}ephemient
04/28/2021, 12:58 AMDavid Smith
04/28/2021, 1:00 AMx.hash is actually calling x.hashCode() ? I.e. x.hashCode() is evaluated every time I call x.hash?David Smith
04/28/2021, 1:02 AMval hash = this.hashCode() would set the value of hash by calling hashCode()ephemient
04/28/2021, 1:06 AMx.hash is actually calling x.getHash(), it is still caching hashCode()ephemient
04/28/2021, 1:06 AMoverride val hash: Int
get() = hashCode()
then x.hash (a.k.a. x.getHash()) would be calling hashCode() every time