Can I mark this interface as `@Stable`? The interf...
# compose
f
Can I mark this interface as
@Stable
? The interface is defined like this:
Copy code
interface SomeState {
  val propertyA: NonStableTypeA
  val propertyB: NonStableTypeB
}
And in the implementation class, all public properties are backed by
MutableState
:
Copy code
internal class SomeStateImpl: SomeState {
  override var propertyA: NonStableTypeA by mutableStateOf(...)
    internal set
  override var propertyB: NonStableTypeB by mutableStateOf(...)
    internal set
}
👍 1
m
yes, you can
f
Why? According to the document:
```* 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.```
Since some of its public property types are unstable, so the interface itself is also unstable.
The implementation class
SomeStateImpl
is stable. But since it also implements another interface for internal usage, I don’t want to expose it directly.
Is exposing the implementation class directly the only way to communicate the stability information to the compiler?
a
The “public property types” here refers to the types of the backing fields, which in your case are `MutableState`s. Stability can’t be inferred solely from the interface definition but if you know that its implementations are stable, you can mark it stable.
👀 2
☝🏼 1
☝🏻 1
Copy code
@Composable
fun SomeFun(state: SomeState)
If you have a composable like this, actually the stabilities of `SomeState`’s implementations are irrelevant here. The metrics of the function will solely depend on the stability of the interface itself.
today i learned 1
z
Putting
@Stable
on an interface isn't just a contract for consumers, it's also a contract to all implementers is they must also ensure they are stable.
👍 1
👍🏻 1