Has there been any talks to add a sort of middle g...
# announcements
t
Has there been any talks to add a sort of middle ground to data classes with arrays, such that you can have the generated implementation plus any necessary additional changes to compensate for arrays?
t
I'm not familiar with this issue.
t
Example being, say,
fun hashCode(dataComputed: Int) = dataComputed + myIntArray.sum()
Versus the warning IntelliJ gives. Something along the lines of
array in data class, consider overriding hashCode
t
I see. Interesting.
h
Use
List
instead?
t
It's interesting that IntelliJ knows about this problem, but it hasn't just been fixed in Kotlin. (Shouldn't be that hard, right?) Do you know if there's anything blocking a fix?
t
I believe any Collection will cause it but I could be mistaken. I could also see a use case for an annotation to exclude a field from hashCode much like you can exclude fields from serialization. It may already do that, I’m not sure. The goal of
data
is to reduce boilerplate, and potentially add future optimization, but currently it doesn’t reduce boilerplate, more so that it removes it entirely. I wouldn’t say it’s necessarily a problem, would just be cool to have additional functionality to extend upon generated functions. It could be part of a greater feature...
extend(res: Int) fun hashCode() = res + myArray.sum()
or something
It looks like an alternative could be to add a secondary constructor and put non-serializables and collections in the body of the class but meh... kind of messy.
j
collections terminate deepHashCode. if they contain arrays they become unqiue hashes. i wash Any? with these so i can get content-based the "deep"{equals,toString,hash}
Copy code
fun arrayOfAnys(it: Array<Any?>): Array<Any?> = deepArray(it) as Array<Any?>

tailrec fun deepArray(inbound: Any?): Any? =
    if (inbound is Array<*>) inbound.also {
        it.forEachIndexed { i, v ->
            (it as Array<Any?>)[i] = deepArray(v)
        }
    }
    else if (inbound is Iterable<*>) deepArray(inbound.map { it })
    else inbound
my usecases are mostly tuples and arrays that collect List sideeffects from map function. this could be destructive to strongly typed long lived instances.