Erlan Amanatov
04/05/2023, 4:14 AM@Stable
annotation properly?
From documentation
The result ofSo, should I overridewill always return the same result for the same two instances.equals
equals
and hashcode
methods for the following class?
@Stable
class State (
text: String,
enabled: Boolean
) {
var text by mutableStateOf(text)
var enabled by mutablesStateOf(enabled)
}
I am not sure because I see a lot of Stable
annotated classes where those methods are not overriddenephemient
04/05/2023, 4:32 AMequals
, that will not be a valid type for `@Stable`:
val state1 = State("1", false)
val state2 = State("1", false)
state1 == state2
state2.text = "2"
state1 != state2
Erlan Amanatov
04/05/2023, 4:39 AMStable
?state1
and state2
is not the same 🤔ephemient
04/05/2023, 5:44 AM@Stable
type:
val state1 = rememberScrollState(initial = 0)
val state2 = rememberScrollState(initial = 0)
state1 != state2 // regardless of whether state1.value == state2.value
Erlan Amanatov
04/05/2023, 5:50 AMequals
for Stable
annotated classes.
What about @Immutable
annotation? As far as I understand, in that case we have to override those methods (considering that all other requirements are met)ephemient
04/05/2023, 6:11 AM@Stable class Keyed(val key: String, private val map: SnapshotStateMap<String, String>) {
var value: String by map
override fun equals(other: Any?): Boolean = other is Keyed && key == other.key && map == other.map
override fun hashCode(): Int = key.hashCode() * 31 + map.hashCode()
}
val map = remember { mutableMapOf("foo" to "hello", "bar" to "world") }
val state1 = Keyed("foo", map)
val state2 = Keyed("foo", map)
val state3 = Keyed("bar", map)
state1 == state2; state1 != state3; state2 != state3 // regardless of anything value is set to
Colton Idle
04/05/2023, 4:33 PM