Is there any default between @Stable and @Immutable when there's no annotation in data class? I wond...
c
Is there any default between @Stable and @Immutable when there's no annotation in data class? I wonder I really need them.
h
I don't think there is any default annotation for a data class. Stable and Default are used to provide better optimizations during recompositions. If a class is marked as immutable, it means a promise from the developer that publicly accessible fields will not change after the instance is constructed and compose compiler can skip recomposition based on the class public parameters. Further, marking a class as stable means if we pass the same parameters the result will always be the same. Hence in those cases, compose compiler can skip recompositions of stable classes or functions as long as parameter list remains the same. Answering your question specifically, while it is not necessary to use them but using them will give you better performance and prevent unexpected recompositions.
z
Most immutable data classes containing only stable types should be inferred as stable if they aren’t explicitly annotated, as long as they are in a module processed by the compose compiler.
The compose compiler will try to infer stability for any classes it processes, so typically you don’t need to use explicit annotations. They are more useful on interfaces as both an explicit contract requirement for implementers and a hint to the compiler that all subtypes should be treated as stable.
w
@Zach Klippenstein (he/him) [MOD] And what about
sealed interface
? Should we mark them as
@Immutable
/
@Stable
even if all implementations are marked as stable?
z
I don’t know if the compiler will propagate stability to sealed parents. Should be possible to verify by checking the compiler report for a function that accepts the sealed type.
👍 1