If I am creating my own state holder, and I make a...
# compose
s
If I am creating my own state holder, and I make an interface for it, let's say a simplified version of it being smth like:
Copy code
@Stable
internal interface MyStateHolder<T> {
  val value: T
  fun operateOnTheStateHolder(newValue: Input)
}
And I then create a
fun
to create an instance of it, smth like:
Copy code
fun <T> MyStateHolder(
  someInput: T
): MyStateHolder<T> = object : MyStateHolder {
  override var value: T by mutableStateOf(someInput)
    private set

  override operateOnTheStateHolder(newValue: Input) {
    ...
  }
}
Is this enough for me to still be able to create an instance of it and still have it be stable properly? Or must I create a
data class
which implements my interface in order for it to have the equals/hashCode methods implemented? (Or without
data
and do it myself)?
Looking at official examples, like PagerState, they seem to create a real class for it instead of an anonymous object https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]compose/foundation/pager/PagerState.kt;l=107-143?q=PagerState But they do not do anything for the equals function, nor is it a data class, hmm The same applies to the
abstract class PagerState
too https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]idx/compose/foundation/pager/PagerState.kt;l=148?q=PagerState
z
It doesn't matter – if the interface is annotated
@Stable
, and that's the type used in the composable function signatures, then the compiler will treat it as stable no matter what the implementation is
thank you color 1
The compiler can't know what the concrete type will be at runtime anyway
s
So it will assume so, and it's up to me to implement the implementation properly in order to not break the contract of @Stable which I am saying that I should be adhering to. So if I just have snapshot backed mutable state in there and/or purely immutable vals, I should just be all good. Just like the PagerState and TextFieldState seem to also do after looking at them.
z
Yep. Once an interface is marked as stable, it's part of that interface's contract, and up to any implementors to comply.
thank you color 1
106 Views