I have a module with many different classes, and I...
# getting-started
y
I have a module with many different classes, and I have a
class AST
which contains some combination of these. I want to compare two instances of
AST
for equality. the catch is that there's also a particular property that may exist in each class, which should be ignored for this comparison. what is the least painful way to do this?
h
Use poko with Poko.Skip: https://github.com/drewhamilton/Poko
y
this looks great. and sadly also like something mgmt would disagree with adding for the purposes of a single feature PR, which is where I'm coming from
(because it flat-out replaces
data class
which we use extensively)
hmm. maybe if I suggest it for that specific module.
k
I don't think Poko would be all that useful. It will allow you to skip a property from one of your constituent classes when creating an equals method for that particular class. But you can easily do that with data classes:
Copy code
@Poko class A(
    val a: Int
    @Skip val b: Int
)

--->

data class private constructor A(val a: Int) {
    val b: Int

    constructor(val a: Int, val b: Int) : this(a) { this.b = b }
}

OR:
data class A(val a: Int, val b: Int) {
    override fun equals...
    override run hashCode...
}
Now if what you need is that the property should be skipped only in class AST then neither Poko nor data classes will help.
y
@Klitos Kyriacou so.... a few questions here 1.
this(a) { this.b = b }
inline
init
syntax is new to me! so many times I've tried reaching for something like this and as alternative, had to add a companion object with a named fun (or an overload of
invoke
). so thank you for that. 2. ...sadly, this doesn't seem to work? given this playground, I'm getting "property must be initialized or be abstract" for the
val b
here. plus the usual deprecation warning for using a non-public primary constructor in a data class. 3. if the name of the field that should be ignored is
b
throughout the entire hierarchy of structs, can reflection help here?
k
1. It's not really "inline init" - it's not an init block, it's just a standard constructor body syntax. 2. You're right, sorry about that, I just typed without testing. 3. I wouldn't recommend reflection as it can slow things down. A custom
equals
and
hashCode
would be better.
y
thank you.