0

first, I'm a kotlin neebie ^^. I want to compare to objects from a data class. But the objects have variables that can be changed. Is the code example a good practice to solve this or is there a problem that i can't see?

Ty

data class Test1(val id : Int, var name: FlexibleProperty<String>)

class FlexibleProperty<T>(var value: T) {
    override fun equals(other: Any?) = true
    override fun hashCode() = 1
}

fun main() {
    val test1 = Test1(1, FlexibleProperty("Hans"))
    val test2 = test1.copy()
    println("test1 == test2 ${test1 == test2}")
    println("test1 === test2 ${test1 === test2}")

    test2.name = FlexibleProperty("Dieter")

    println("test1 == test2 ${test1 == test2}")
    println("test1 === test2 ${test1 === test2}")
}

EDIT:// Sry, I was a little confused ^^. My detailed problem is: I want to add these objects into a set. If I use normal string variables, the objects are different, so the set has 2 objects. But if I add test1 and check set.contains(test2) with my FlexiableProperty, the result is true, so I have to update the object. I don't want to check the id outside of the objects (with maybe a map and the id as key)

Here the code snippet with a set:

data class Test1(val id : Int, val name: FlexibleProperty<String>)

data class FlexibleProperty<T>(var value: T) {
    override fun equals(other: Any?) = true
    override fun hashCode() = 1
}

fun main() {
    val test1 = Test1(1, FlexibleProperty("Hans"))
    val test2 = test1.copy(name = FlexibleProperty("Dieter"))

    val setTest = mutableSetOf(test1)
    if (setTest.contains(test2)) {
        setTest.remove(test1)
    }
    setTest.add(test2)

    println("set $setTest")
}
user2558928
  • 269
  • 1
  • 2
  • 9
  • What is exactly the problem? What's the issue of comparing objects that contain `var`iables? – gpunto Aug 29 '19 at 10:16
  • I want to add these objects into a set. If I use normal string variables, the to objects are different, so the set has 2 objects. But if I add test1 and check set.contains(test2), the result is true, so I have to update the object. I don't want to check the id outside of the objects – user2558928 Aug 30 '19 at 05:03
  • Sorry, but I still don't get your question. Should Test1("Hans") and Test1("Dieter") be same objects or not in the set? – Alexey Soshin Aug 30 '19 at 08:52
  • It's the same object, but the name has been changed (by a user input for example). – user2558928 Aug 30 '19 at 19:33

1 Answers1

0

There's no specific problem with your solution per see, but it could be greatly improved.

First, name can still be a value, since you use copy() anyway:

 data class Test1(val id : Int, val name: FlexibleProperty<String>)

 val test2 = test1.copy(name = FlexibleProperty("Dieter"))

Having no mutable properties make your class thread safe, and easier to reason about.

Second, when you use data class at the top level, it makes a lot of sense to make all classes it encapsulates also data classes. That would also solve your second problem with the need of overriding equals and hashCode:

data class FlexibleProperty<T>(var value: T) 

Also, there's no reason to check referential equality with ===, at least with the examples you provide.

Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
  • thanks a lot. you helped. I have updated my question a little bit, so maybe there is a better solution for my problem... – user2558928 Aug 30 '19 at 05:10