y
06/09/2025, 8:18 AMclass 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?hfhbd
06/09/2025, 8:30 AMy
06/09/2025, 8:39 AMy
06/09/2025, 8:40 AMdata class
which we use extensively)y
06/09/2025, 8:40 AMKlitos Kyriacou
06/09/2025, 10:31 AM@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
06/09/2025, 10:44 AMthis(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?Klitos Kyriacou
06/09/2025, 10:50 AMequals
and hashCode
would be better.y
06/09/2025, 11:24 AM