Kacper
01/10/2024, 2:19 PM@Stable
annotation there is this contract:
When applied to a class or an interface, Stable indicates that the following must be true:
1. The result of equals will always return the same result for the same two instances.
2. When a public property of the type changes, composition will be notified.
3. All public property types are stable.
Point 2 and 3 are easy to understand, but the 1st one, not so much. There is also this, written in compose-api-guidelines.md:
Jetpack Compose framework development, Library development and App development MUST ensure in custom implementations of .equals() for @Stable types that for any two references a and b of @Stable type T, a.equals(b) MUST always return the same value. This implies that any future changes to a must also be reflected in b and vice versa.
If someone who understands this, could rephrase it or maybe show an example it would help as I am quite confused.ephemient
01/10/2024, 2:33 PMval a = mutableListOf(1)
val b = mutableListOf(1)
a == b
b[0] = 2
a != b
MutableList is not stableephemient
01/10/2024, 2:34 PMAlbert Chang
01/10/2024, 2:48 PMequals()
overridden or not.
For a mutable class to satisfy rule 1, its equals()
must not be overridden (to be based on its content). With the default equals()
based on object identity, the result of equals will only be true if the two instances are the same object, and the result will always be true no matter how the content of the object changes. The same goes for when the result is false.Kacper
01/10/2024, 4:15 PMclass Wrapper(int: Int) {
var int: Int
}
and I make 2 instances
val w1 = Wrapper(1)
val w2 = Wrapper(1)
equals should return false
w1 != w2
And this class passes the 1st rule. But for the same class but in the data class syntax:
data class Wrapper(val int: Int)
As data class generates equals based on the content inside, the above equals check will be true and the 1st criteria is not met. Am I right?
So can we just conclude that the first criteria is just that the objects, when equal (equals returns true) are the same object? So the references point to the same place in memory? Or are there other instances where this is true and the references do not point in the same place?Albert Chang
01/10/2024, 4:19 PMw1 == w2
is true and will always be true because the content of the two instances will never change.Kacper
01/10/2024, 4:21 PMKacper
01/10/2024, 4:27 PM