Hi, in <documentation> of `@Stable` annotation the...
# compose
k
Hi, in documentation of
@Stable
annotation there is this contract:
Copy code
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:
Copy code
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.
e
Copy code
val a = mutableListOf(1)
val b = mutableListOf(1)
a == b
b[0] = 2
a != b
MutableList is not stable
but if you had a wrapper that used identity comparison on the underlying list, that could be
a
Basically an immutable class always satisfies rule 1, whether it has
equals()
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.
k
@Albert Chang So... for example, if there is a wrapper
Copy code
class Wrapper(int: Int) {
    var int: Int
}
and I make 2 instances
Copy code
val w1 = Wrapper(1)
val w2 = Wrapper(1)
equals should return false
Copy code
w1 != w2
And this class passes the 1st rule. But for the same class but in the data class syntax:
Copy code
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?
a
No, the data class also meets the criteria, because it’s immutable.
w1 == w2
is true and will always be true because the content of the two instances will never change.
k
Damn, the var magically changed to val 😄 Yes, in the above case (with my silly mistake) its an immutable object.
@Albert Chang I understand it better now, thanks for everything