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

Peng Wang

04/10/2022, 12:07 PM
https://developer.android.com/reference/kotlin/androidx/compose/runtime/Immutable
the doc of
@Immutable
said that “data classes that only contain val properties that do not have custom getters can safely be marked as Immutable if the types of properties are either primitive types or also Immutable” I thought the data class with val properties should have been treated as Stable (or Immutable) by Compiler , isn’t it ? if I don’t annotate
Person
with `@Immutable`,means the
PersonView
can’t skip the unnecessary Recomposition?
❤️ 1
a

Albert Chang

04/10/2022, 1:14 PM
If the types of val properties are all known to be stable, the compiler will infer the class as stable.
p

Peng Wang

04/10/2022, 1:18 PM
so the
@Immutable
of
Person
in the sample doesn’t make any sense, right? https://developer.android.com/reference/kotlin/androidx/compose/runtime/Immutable
p

Paul Woitaschek

04/10/2022, 1:53 PM
Stable != Immutable. The compiler would infer it as stable but not as immutable
👍 1
Also all this implies that you enabled compose for that module. So if you consume a class that is located in a module that does not have the compose plugin applied, the stability will not be inferred at all
p

Peng Wang

04/10/2022, 2:00 PM
In terms of smart recomposition, what’s the difference between Immutable and Stable ?
The compiler can infer the stability but it doesn't make manually marking classes pointless. You can see that all classes that are stable are manually marked in the framework. Marking a class ensures its stability and there are also some cases where the compiler can't infer the stability.
p

Paul Woitaschek

04/10/2022, 3:10 PM
Oh that's interesting
I so wished I didn't need to mark everything containing a list or a platform class as stable
p

Peng Wang

04/10/2022, 5:34 PM
I’m used to using data class to define my UiState, but I’ve never marked
Immutable
, and also I rarely see it in other people’s code. What I want to know is whether
Immutable
should always be marked for data class, even if it is stable. What is the bad case if don’t mark it for the data class? @Albert Chang
p

Paul Woitaschek

04/10/2022, 5:38 PM
He already quoted that stable has the same effect as Immutable. If it can't be inferred as stable it will cause recompositons
p

Peng Wang

04/10/2022, 6:03 PM
the the
Person
of official sample, is stable itself right? why it still needs to be marked as
Immutable
?
p

Paul Woitaschek

04/10/2022, 6:15 PM
It's correct to mark it as Immutable but it's not necessary. But if I understand correctly there is a distinction in the definition of immutable and stable that might be used one day by compose
h

hfhbd

04/11/2022, 4:10 AM
Sooo, if you have another module containing only data classes in your project without compose, do you still need to mark them manually, (or apply the compose plugin)?
a

Albert Chang

04/11/2022, 4:13 AM
You need to apply compose compiler plugin. Even if you mark them manually, if you don’t apply the compiler plugin, they will still be treated as unstable in other modules.
2 Views