https://kotlinlang.org logo
#compose
Title
# compose
f

fengdai

05/25/2022, 6:27 AM
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

myanmarking

05/25/2022, 8:55 AM
yes, you can
f

fengdai

05/25/2022, 9:21 AM
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

Albert Chang

05/25/2022, 10:26 AM
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

Zach Klippenstein (he/him) [MOD]

05/25/2022, 3:06 PM
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
26 Views